SpringBoot集成Swagger2 的3.0版本

一、导入依赖

<!--springfox-boot-starter  包含springfox-Swagger2-3.0 和 springfox-Swagger-ui-3.0-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>

二、配置SwaggerConfig使能Swagger2

@Configuration
@EnableSwagger2
public class SwaggerConfig {
}

这里仅仅是让他能使用,并没有做任何自定义配置,走的默认配置

三、在主配置文件中添加配置(Springboot2.6以下版本的话可以不用配置)

spring:
  mvc:
    pathmatch:
      matching-strategy: ant_path_matcher

四、在主启动类上添加@EnableOpenApi注解

@EnableOpenApi
@SpringBootApplication
public class SpringBoot07SwaggerApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBoot07SwaggerApplication.class, args);
    }

}

五、启动项目,访问http://localhost:8080/swagger-ui/index.html

在这里插入图片描述


解释

Springboot2.6以上版本如果不在yaml中配置,直接启动,会报错

Failed to start bean ‘documentationPluginsBootstrapper’; nested exception is java.lang.NullPointerEx

原因:Springboot2.6以后将SpringMVC 默认路径匹配策略从AntPathMatcher 更改为PathPatternParser,导致出错,
我用的是Springboot2.7.4,2.6以下版本的话可以不用配置

访问路径的变化
观察一下springfox-Swagger-ui-3.0的目录结构
在这里插入图片描述
如果还访问之前的swagger-ui.html的话,就只能404了

所以在主启动类上添加@EnableOpenApi 注解并访问http://localhost:8080/swagger-ui/index.html

但这个时候,如果添加的依赖是

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>3.0.0</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>3.0.0</version>
</dependency>

这个注解会报错,因为没有,所以直接使用下面这个依赖就好了,他同时也包含了这两个依赖

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

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

霍志杰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值