spring boot 集成Swagger

本文介绍了如何在Spring Boot项目中集成Swagger,包括添加pom依赖,配置Swagger类,以及展示Swagger-ui。在遇到swagger-ui无法显示时,排查了可能的原因,如确认依赖已添加,解决版本不匹配问题,并提供了Spring Boot 2.7.0适配的解决方案。

添加pom 依赖

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

添加Swagger 配置类

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket productApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))  //添加ApiOperiation注解的被扫描
                .paths(PathSelectors.any())
                .build();

    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().title("swagger和springBoot整合").description("swagger的API文档")
                .version("1.0").build();
    }

}

swagger-ui 展示

访问 http://localhost:8080/swagger-ui.html 即可看到对应的UI 界面
在这里插入图片描述

swagger-ui 如果无法显示,排错如下

  1. 找不到swagger-ui 界面,但是 http://localhost:8080/v2/api-docs 访问正常【确认swagger 的两个jar 包是否都添加到了pom 文件中】
  2. 依赖于spring boot 版本不匹配【我这里springboot 版本:2.5.5,配swagger:2.7.0 正好展示,如果spring boot :2.6.7 则显示失败,报错如下,直接降低spring boot 版本即可】
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] 
```

新增内容如下:

最新版springboot 2.7.0 可以和下面两个文件搭配使用
【但是需要将@EnableSwagger2 放到springboot 启动类,另外还需要添加下面的配置文件到application.yml 中】

mvc:
    pathmatch:
      matching-strategy: ant_path_matcher
		<dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.7.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.7.0</version>
        </dependency>```
### Spring Boot 整合 Swagger 实现 API 文档自动化配置 #### 添加依赖项 为了使 Spring Boot 项目能够支持 Swagger 自动生成 API 文档,在项目的 `pom.xml` 文件中需引入相应的 Maven 依赖: ```xml <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency> ``` 此操作确保了 spring-boot-starter-swagger 成为 Spring Boot 生态系统的一部分,实现了与框架的无缝集成并提供快速开发能力[^1]。 #### 配置Swagger属性 接着定义 Swagger 的基本设置。可以在 application.properties 或者 application.yml 中加入如下配置来定制化 Swagger 行为: 对于 properties 文件: ```properties springdoc.api-docs.path=/v3/api-docs springdoc.swagger-ui.path=/swagger-ui.html ``` 对于 YAML 文件: ```yaml springdoc: api-docs: path: /v3/api-docs swagger-ui: path: /swagger-ui.html ``` 这些路径指定了 OpenAPI 描述文件的位置以及 Swagger UI 访问地址[^2]。 #### 创建Swagger配置类 (可选) 如果希望进一步调整 Swagger 设置,则可以创建一个新的 Java 类用于配置 Swagger 特定选项。下面是一个简单的例子展示如何通过此类启用或禁用某些特性: ```java import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; @Configuration public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.OAS_30) .select() .apis(RequestHandlerSelectors.any()) .paths(PathSelectors.any()) .build(); } } ``` 这段代码片段展示了怎样构建一个基于 OAS 3.0 规范的 Docket 对象,并将其应用于整个应用程序中的所有控制器方法上。 完成上述步骤之后,当启动 Spring Boot 应用程序时,就可以通过访问指定 URL 来查看由 Knife4j 提供的增强版 Swagger UI 页面,从而方便地管理和测试 RESTful Web Services 接口。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

阿雷由

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

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

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

打赏作者

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

抵扣说明:

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

余额充值