Springboot 2.3 与swagger 3.0 集成
By:Roy.LiuLast updated:2020-08-24
以前用过SWAGGER 2, 还是比较简单,最近将spring boot 升级到最新的2.3.3,同时也将 swagger 升级到3.0 版本,记录一下基本过程.
在pom.xml 中引入 maven 依赖
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.3.RELEASE</version> </parent> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency>
2. swagger 配置类
@EnableOpenApi @Configuration public class Swagger3Config { @Bean public Docket createRestApi() { /* 自定义返回,公共用 List<Response> responseMessageList = new ArrayList<>(); Response response500 = new ResponseBuilder().code("500").description("500").build(); responseMessageList.add(response500); */ /* 自定义公用的参数 List<RequestParameter> globalRequest = new ArrayList<>(); globalRequest.add(new RequestParameterBuilder().name("sign").required(true).description("签名").in(ParameterType.QUERY).build()); */ return new Docket(DocumentationType.OAS_30) .useDefaultResponseMessages(false) //.globalResponses(HttpMethod.GET, responseMessageList) //.globalRequestParameters(globalRequest) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("yihaomen") .description("yihaomen framework......") .contact(new Contact("yihaomen。", "http://www.yihaomen.com", "")) .version("1.0") .build(); } }
3. 为了防止springboot 的拦截,还得将swagger 的 资源加入到不被拦截的列表里面
@Configuration public class AppConfig extends WebMvcConfigurationSupport { @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor(new ClientTokenInterceptor()).addPathPatterns("/api/**") .excludePathPatterns("/swagger-ui/**"); } @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/swagger-ui/**") .addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/"); } }
下面是一个简单的测试类Controller
@Api(value = "测试SwaggerAPI Annotation", tags = {"测试SwaggerAPI"}) @RestController @RequestMapping("/swagger") public class SwaggerController { @GetMapping("/no_params") public String noParams() { return "OK"; } @GetMapping("/one_params") @ApiOperation(value = "one parameters", notes = "一个参数的设计", nickname = "nickname one parameter") public AjaxResult oneParams(String name) { return AjaxResult.success(); } @ApiOperation(value = "两个参数的测试", notes = "两个参数的测试") @ApiImplicitParams( { @ApiImplicitParam(name="name", value="用户名", dataTypeClass = String.class, required = true), @ApiImplicitParam(name="age", value="年龄", dataTypeClass = Integer.class, required = true) } ) @GetMapping("/two_params") public AjaxResult twoParams(String name, int age) { return AjaxResult.success(name + ": " + age); } @ApiOperation(value = "参数在路径中", notes = "自定义返回Response", response = AjaxResult.class) @ApiImplicitParam(name="param", value="参数", example = "tom", dataTypeClass = String.class, required = true) @GetMapping("/path_params/{param}") public AjaxResult paramInPath(@PathVariable String param) { return AjaxResult.success(); } }
检验,启动应用,进入网址:http://localhost:9000/mobile/swagger-ui/index.html , 地址根据本机情况自己改动.
From:一号门
Previous:利用Ansible自动化部署zookeeper集群
COMMENTS