【springboot2.7.5整合swagger3.0.0】
其中,springboot与swagger的版本对应关系见。也可以采用注解的方式实现。配置时注意版本冲突问题。附:可能出现的其他问题。
·
springboot2.7.5整合swagger3.0.0
1. pom.xml添加依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>3.0.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>3.0.0</version>
</dependency>
配置时注意版本冲突问题
swagger集成版本冲突报错
其中,springboot与swagger的版本对应关系见
springboot_swagger各版本整理
2. application.properties中添加配置
为了解决springboot 2.6版本之后出现的与swagger不兼容的问题,需修改springboot处理映射的默认匹配策略,可以考虑通过在application.properties中添加配置实现:
spring.mvc.pathmatch.matching-strategy=ant-path-matcher
也可以采用注解的方式实现swagger版本和springboot 2.6+版本的不兼容问题
3. 添加配置类
package com.xxx.xxx.xxx.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import springfox.documentation.builders.ApiInfoBuilder;
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;
@Configuration
public class Swagger2Configuration extends WebMvcConfigurerAdapter {
@Bean
public Docket docket() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.enable(true)
.groupName("v1")
.select()
// 过滤路径
//.paths(PathSelectors.ant())
// 指定扫描的包
.apis(RequestHandlerSelectors.basePackage("com.xxx.xxx.xxx.controller"))
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder().title("Swagger 测试接口文档")
.description("Spring Boot 集成 Swagger 测试接口文档")
.contact(new Contact("xxx", null, "123456@gmail.com"))
.version("v1.0")
.build();
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("swagger-ui.html")
.addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/");
super.addResourceHandlers(registry);
}
}
注:如果你的代码中添加了拦截器,还需进行如下配置:
//防止拦截器对swagger的相关请求进行拦截
notFilter.add("/v2/api-docs");
notFilter.add("/swagger-resources/configuration/ui");
notFilter.add("/swagger-resources");
notFilter.add("/swagger-resources/configuration/security");
// 旧版本swagger的访问方式
// notFilter.add("/swagger-ui.html");
// swagger页面
notFilter.add("/swagger-ui/");
springboot整合swagger被拦截问题
各路径的功能如下:
在Spring Boot中配置Swagger时,需要将一些特定的路径设置为不拦截,以确保Swagger能够正常使用。这些路径的作用如下:
1. `/v2/api-docs`: 这个路径是Swagger生成API文档的接口。Swagger会通过访问这个接口获取API文档的信息,然后展示在Swagger UI中。
2. `/swagger-resources/configuration/`: 这个路径用于获取Swagger配置的信息,包括Swagger UI的相关配置。
3. `/swagger-resources/`: 这个路径用于获取Swagger的资源文件,例如CSS、JavaScript等文件。
4. `/swagger-resources/configuration/security`: 这个路径用于获取Swagger安全配置的信息。Swagger可以配置安全规则,例如需要身份验证才能访问API文档。
5. `/swagger-ui.html`: 这个路径是Swagger UI的入口页面,通过访问这个路径可以打开Swagger UI界面。
将这些路径设置为不拦截,可以确保Swagger能够正常访问这些接口和资源,从而展示API文档和提供交互式的API测试功能。如果这些路径被拦截,Swagger将无法获取到必要的信息,导致无法正常使用。
4.在controller中添加注解
package com.xxx.xxx.xxx.controller;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
@Api
@RestController
public class SwaggerDemoController {
@ApiOperation(value = "hello world 接口")
@GetMapping("hello")
public String hello() {
return "hello world";
}
@ApiOperation(value = "有参接口")
@PostMapping("demo")
public String demo(@ApiParam(name = "name", value = "xxx", required = true) String name) {
return "hello," + name;
}
}
各注解的含义:
swagger常用注解含义
5.访问页面
通过访问路径http://localhost:端口号/swagger-ui/得到如下界面:
发现接口信息不显示,排查原因,发现是路径问题,修改包名,即可正常显示接口:
附:可能出现的其他问题
新版本swagger 3.0可能出现的问题
无限请求问题

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐
所有评论(0)