# springboot 2.6.5引入swagger3 相关配置
一 、maven中引入
<!--swagger 3-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
二、application.yml配置
spring:
mvc:
pathmatch:
matching-strategy: ant_path_matcher
原因:SpringMVC 默认路径匹配策略从AntPathMatcher 更改为PathPatternParser,导致出错
三、SecurityConfig放行:
//swagger3
.antMatchers("/swagger-resources/**").permitAll()
.antMatchers("/swagger-ui/**").permitAll()
.antMatchers("/v3/**").permitAll()
四、自定义配置swagger
package com.sangeng.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
/***
* @author Casear
* @date
* @apiNote
*/
@EnableOpenApi
@Configuration
public class SpringFoxSwaggerConfig {
/**
* 配置基本信息
* @return
*/
@Bean
public ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Swagger Test App Restful API")
.description("swagger test app restful api")
.termsOfServiceUrl("https://github.com/Caesar454905970")
.contact(new Contact("Caesar","https://www.cnblogs.com/chengqiang521/","454905970@qq.com"))
.version("1.0")
.build();
}
/**
* 配置文档生成最佳实践
* @param apiInfo
* @return
*/
@Bean
public Docket createRestApi(ApiInfo apiInfo) {
return new Docket(DocumentationType.OAS_30)
.apiInfo(apiInfo)
.groupName("SwaggerGroupOneAPI")
.select()
.apis(RequestHandlerSelectors.withClassAnnotation(RestController.class))
.paths(PathSelectors.any())
.build();
}
}
五、和swagger2不同的是,启动类上的@EnableSwagger2可以注释掉
//@EnableSwagger2
六、使用
@RestController
@RequestMapping("/user")
@Api(tags = "我是测试swagger3")
public class UserController {
@Autowired
private UserService userService;
/**
* 个人信息查询接口
* @return
*/
@SystemLog(businessName="个人信息查询接口")
@GetMapping("/userInfo")
@ApiOperation("个人信息查询接口:swagger3")
public ResponseResult userInfo(){
return userService.userInfo();
}
七、浏览地址:http://localhost:8081/swagger-ui/
参考文章
https://blog.csdn.net/sluck_0430/article/details/124179540
【springboot-swagger2升级到3】https://www.bilibili.com/video/BV19Q4y1k7K9?vd_source=8f2f0e5aab9462dfb31809558ee38799