1. 引言
在写完接口之后,都需要测试一下是否正确,但是有的时候不想写字段,超多字段好难受,那就用Swagger2吧!
2. Maven依赖
1 2 3 4 5 6 7 8 9 10
| <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.8.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.8.0</version> </dependency>
|
3. Swagger2配置类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Profile; import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.cors.UrlBasedCorsConfigurationSource; import org.springframework.web.filter.CorsFilter; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Profile({ "default", "dev" }) @Configuration @EnableSwagger2 public class Swagger2 {
/** * SwaggerAPI跨域过滤器 */ @Bean public CorsFilter swaggerAPIFilter() { final UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource(); final CorsConfiguration corsConfiguration = new CorsConfiguration(); corsConfiguration.setAllowCredentials(true); corsConfiguration.addAllowedOrigin("*"); corsConfiguration.addAllowedHeader("*"); corsConfiguration.addAllowedMethod("*"); urlBasedCorsConfigurationSource.registerCorsConfiguration("/v2/**", corsConfiguration); return new CorsFilter(urlBasedCorsConfigurationSource); }
/** * Swagger配置 */ @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select() .apis(RequestHandlerSelectors.basePackage("com.hj.project.controller")).paths(PathSelectors.any()) .build(); }
private ApiInfo apiInfo() { return new ApiInfoBuilder().title("project接口").description("project接口") .termsOfServiceUrl("localhost:8080/hello").version("1.0").build(); } }
|
有时候需要传Token值,那么就需要在配置类里边先传入Token
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| @Bean public Docket createRestApi() { // 添加head参数start ParameterBuilder tokenPar = new ParameterBuilder(); List<Parameter> pars = new ArrayList<Parameter>(); tokenPar.name("X-Token").description("令牌").modelRef(new ModelRef("string")).parameterType("header") .defaultValue(token) .required(false).build(); pars.add(tokenPar.build()); // 添加head参数end return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select() .apis(RequestHandlerSelectors.basePackage("com.hj.project.controller")).paths(PathSelectors.any()).build() .globalOperationParameters(pars); }
|
3. 访问
http://localhost:8080/swagger-ui.html 这样你就配好了!!!
如果你喜欢这篇文章,请点个赞,加个关注吧!!!