Swagger進行介面測試
阿新 • • 發佈:2020-09-12
Swagger進行介面測試
1、匯入依賴
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>io.swagger</groupId> <artifactId>swagger-annotations</artifactId> <version>1.5.20</version> <scope>compile</scope> </dependency>
2、配置Swagger基本資訊(一般不用修改)
package com.wagn.config; import com.google.common.base.Predicates; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.service.Contact; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket webApiConfig(){ return new Docket(DocumentationType.SWAGGER_2) .groupName("webApi") .apiInfo(webApiInfo()) .select() .paths(Predicates.not(PathSelectors.regex("/admin/.*"))) .paths(Predicates.not(PathSelectors.regex("/error.*"))) .build(); } private ApiInfo webApiInfo(){ return new ApiInfoBuilder() .title("網站-課程中心API文件") .description("本文件描述了課程中心微服務介面定義") .version("1.0") .contact(new Contact("王永豐", "http://hbnu.fun", "[email protected]")) .build(); } }
3、如過當前模組被其他模組依賴,其他模組也需要Swagger,則需要在其他模組的啟動類上添加註釋
@ComponentScan(basePackages = {"com.wagn"})//修改掃描規則
(如果是當前模組就不用註釋了)