Spring Boot 使用 springfox-boot-starter 接入 Swagger2
Spring Boot Swagger 大约 3094 字添加依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
可能出现的错误
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.19.jar:5.3.19]
at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:54) ~[spring-context-5.3.19.jar:5.3.19]
at org.springframework.context.support.DefaultLifecycleProcessor$LifecycleGroup.start(DefaultLifecycleProcessor.java:356) ~[spring-context-5.3.19.jar:5.3.19]
at java.base/java.lang.Iterable.forEach(Iterable.java:75) ~[na:na]
at org.springframework.context.support.DefaultLifecycleProcessor.startBeans(DefaultLifecycleProcessor.java:155) ~[spring-context-5.3.19.jar:5.3.19]
at org.springframework.context.support.DefaultLifecycleProcessor.onRefresh(DefaultLifecycleProcessor.java:123) ~[spring-context-5.3.19.jar:5.3.19]
at org.springframework.context.support.AbstractApplicationContext.finishRefresh(AbstractApplicationContext.java:935) ~[spring-context-5.3.19.jar:5.3.19]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:586) ~[spring-context-5.3.19.jar:5.3.19]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) ~[spring-boot-2.6.7.jar:2.6.7]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:740) ~[spring-boot-2.6.7.jar:2.6.7]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:415) ~[spring-boot-2.6.7.jar:2.6.7]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:303) ~[spring-boot-2.6.7.jar:2.6.7]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1312) ~[spring-boot-2.6.7.jar:2.6.7]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1301) ~[spring-boot-2.6.7.jar:2.6.7]
at com.example.learnswagger2.LearnSwagger2Application.main(LearnSwagger2Application.java:11) ~[classes/:na]
原因
版本限制:Spring Boot 2.6.0
后会出现问题,具体可参考如下链接:
https://github.com/springfox/springfox/issues/3462
解决方法一
添加@EnableWebMvc
注解。
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@EnableWebMvc
@SpringBootApplication
public class LearnSwagger2Application {
public static void main(String[] args) {
SpringApplication.run(LearnSwagger2Application.class, args);
}
}
解决方法二
配置文件中增加如下配置。
spring:
mvc:
pathmatch:
matching-strategy: ant_path_matcher
访问地址
Spring Boot
监听了8080
端口。
http://localhost:8080/swagger-ui/index.html
http://localhost:8080/v2/api-docs
生产环境关闭
springfox:
documentation:
enabled: false
备注
springfox
已迁移到springdoc
。
GitHub
阅读 1472 · 发布于 2022-09-24
————        END        ————
Give me a Star, Thanks:)
https://github.com/fendoudebb扫描下方二维码关注公众号和小程序↓↓↓

昵称:
随便看看
换一批
-
Android 根据包名启动 Activity阅读 3132
-
Spring Boot Thymeleaf 设置模板过期时间阅读 107
-
算法:解析中缀表达式阅读 1284
-
Spring Boot NoClassDefFoundError: com/mongodb/connection/DefaultClusterFactory阅读 6802
-
Java jstat 监控远程服务阅读 1695
-
Spring Boot 返回加密后的 Response阅读 505
-
使用 Java8 压缩文件阅读 2725
-
MongoDB 过期索引(TTL 索引)阅读 4256
-
Go 操作 PostgreSQL阅读 3749
-
Android 底部控件不受输入法影响被顶到上方阅读 1840