swagger3,spring boot集成swagger3,spring boot2.6

spring boot集成swagger3

swagger2的整合​

swagger3的使用步骤和2略有差异

maven依赖

<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>

swagger3.0的依赖将​​springfox-swagger-ui​​​和​​springfox-swagger2​​​整合到了​​springfox-boot-starter​​,还包括一些spring的依赖,说一不需要再去关心其他的依赖。

开启swagger

和2版本一样,需要一个swagger的配置,用于配置基本信息

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.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

/**
* swagger配置
*/
@Configuration
public class SwaggerConfig {

Boolean swaggerEnabled = true;

@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.OAS_30)
.apiInfo(apiInfo())
// 是否开启
.enable(swaggerEnabled)//true
.select()
.apis(RequestHandlerSelectors.basePackage("com.clc.resource"))
.paths(PathSelectors.any())
.build();
}

private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("资源中心swagger业务")
//创建人
.contact(new Contact("clc", "http://www.baidu.com", "clc@x.com"))
.version("1.0")
.description("不需要描述")
.build();
}
}

默认访问地址

​http://localhost​​:port/swagger-ui/index.html


和2版本地址不同:​http://localhost:port/swagger-ui.html

spring boot 2.6.x 版本配置

如果是spring boot 2.6.x集成了swagger,会出现异常信息:

Failed to start bean 'documentationPluginsBootstrapper';

org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException
at org.springframework.context.support.DefaultLifecycleProcessor.doStart(DefaultLifecycleProcessor.java:181) ~[spring-context-5.3.14.jar:5.3.14]
at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:54) ~[spring-context-5.3.14.jar:5.3.14]
at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:356) ~[spring-context-5.3.14.jar:5.3.14]
at org.springframework.context.support.DefaultLifecycleProcessor$$Lambda$627/1737023457.accept(Unknown Source) ~[na:na]
at java.lang.Iterable.forEach(Iterable.java:75) ~[na:1.8.0_31]
at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:155) ~[spring-context-5.3.14.jar:5.3.14]
at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:123) ~[spring-context-5.3.14.jar:5.3.14]
at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:935) ~[spring-context-5.3.14.jar:5.3.14]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:586) ~[spring-context-5.3.14.jar:5.3.14]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) ~[spring-boot-2.6.2.jar:2.6.2]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:730) [spring-boot-2.6.2.jar:2.6.2]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:412) [spring-boot-2.6.2.jar:2.6.2]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:302) [spring-boot-2.6.2.jar:2.6.2]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1301) [spring-boot-2.6.2.jar:2.6.2]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1290) [spring-boot-2.6.2.jar:2.6.2]

因为Springfox使用的路径匹配是基于AntPathMatcher的,而Spring Boot 2.6.X使用的是PathPatternMatcher

解决方案

加spring配置指定AntPathMatcher

spring.mvc.pathmatch.matching-strategy: ANT_PATH_MATCHER

使用示例

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
* ClassName: JSR303Controller
* Description:JSR303协议测试
*
* @author chengluchao
* @date: 2022/1/11 14:49
* @since 1.3.9
*/
@Api("JSR303协议")
@Validated
@RestController
@RequestMapping("/jsr303")
@Slf4j
public class JSR303Controller {

@ApiOperation("批量保存数据")
@ResponseBody
@PostMapping("batchSave")
public Object batchSave(@RequestBody @Validated(ValidationGroupSequence.class) BatchSysGroupDto dto) {
return "Result.okInstance();";
}
}

效果

spring boot集成swagger3_spring

swagger3.0新功能

  1. Webflux支持
  2. Spring Integration支持
  3. SpringBoot支持springfox Boot starter依赖性(零配置、自动配置支持)
  4. 支持OpenApi 3.0.3

CLC