spring boot 2.7.9 整合 Swagger 3.0

运行环境:

 jdk  1.8

springboot 2.7.9

swagger 3.0.0


Maven引入:

<!-- swagger接口文档 -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>

application.yaml:

spring:
  mvc:
    pathmatch:
      # 引入swagger3.0时加入的配置 localhost:2365/swagger-ui/index.html
      matching-strategy: ant_path_matcher

Swagger配置文件:

package com.ls.config.swaggerConfig;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
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 RosSwaggerConfig {

    @Bean
    public Docket createRestApi(){
        //Docket: 摘要对象,通过对象配置 描述文件的信息
        Docket docket = new Docket(DocumentationType.OAS_30);
        docket.apiInfo(myApiInfo())
                //select():返回ApiSelectorBuilder对象,通过对象调用build()可以创建Docket对象
                .select()
                // 指定要扫描/维护接口文档的包(否则就全部扫描)
                .apis(RequestHandlerSelectors.basePackage("com.ls.api.leakageApi"))
                // 路径过滤:该Docket-UI展示时,只展示指定路径下的接口文档(any表示都展示)
                .paths(PathSelectors.any())
                .build();
        return docket;
    }

    // 接口文档的概要信息,返回ApiInfo对象
    private ApiInfo myApiInfo(){
        //标题
        String title = "ROS系统接口文档";
        //简单描述
        String description = "一个简单明了的接口信息文档";
        //版本
        String version = "V1.0.0";
        // url接口路径前缀
        String termsOfServiceUrl = "/";
        //作者信息
        Contact contact = new Contact("SLIU","","1549865882@qq.com");
        //协议
        String license = "The Apache License";
        //协议url
        String licenseUrl = "https://wwww.baidu.com";

        ApiInfo apiInfo =  new ApiInfoBuilder()
                .title(title)
                .description(description)
                .version(version)
                .termsOfServiceUrl(termsOfServiceUrl)
                .contact(contact)
                .license(license)
                .licenseUrl(licenseUrl)
                .build();
        return apiInfo;
    }

}

效果图:


 引入Swagger遇到的困难:

描述:Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException 没有这个bean,空指针了。

[17:37:45.470] ERROR org.springframework.boot.SpringApplication 821 reportFailure - Application run failed 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.25.jar:5.3.25]
	at org.springframework.context.support.DefaultLifecycleProcessor.access$200(DefaultLifecycleProcessor.java:54) ~[spring-context-5.3.25.jar:5.3.25]

 源码原因:

据网上资料找,3.0的Swagger已经不继承WebMvcConfig这个类,是继承了WebMvcConfigSupport类,从而改动了配置路径规则,然后报空指针,大概是这么个意思,想了解的可以去查一下具体原因。

解决报错的方法:

引入下面的配置,该配置是修改springboot的路径配置规则,springboot路径配置规则有两个,

path_pattern_parser , ant_path_matcher 。我们需要修改成 ant_path_matcher 路径配置规则。
spring:
  mvc:
    pathmatch:
      # 引入swagger3.0时加入的配置 localhost:2365/swagger-ui/index.html
      matching-strategy: ant_path_matcher

补充几点:

1. springfox-boot-starter 3.0.0 版本的swagger是最新版

2. 该版本已经不需要注解在启动文件上,例如:EnableOpenApi,EnableSwagger2等

3.该最新版本的官网资料:Swagger官网资料

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
好的,你想要了解如何在Spring Boot 2.7.9中整合Swagger2并使用com.github.xiaoymin的knife4j扩展库,是吗?我可以为你提供一些步骤。 首先,你需要在你的pom.xml文件中添加以下依赖: ```xml <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-spring-boot-starter</artifactId> <version>2.0.8</version> </dependency> ``` 然后,你需要在你的Spring Boot配置类中添加以下内容: ```java @Configuration @EnableSwagger2 public class Swagger2Config { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("Swagger2接口文档") .description("Swagger2接口文档") .termsOfServiceUrl("http://localhost:8080/") .version("1.0") .build(); } } ``` 这个配置类将创建一个名为"Swagger2接口文档"的文档,并扫描com.example.demo.controller包中的所有控制器。 最后,你可以在你的浏览器中访问http://localhost:8080/doc.html来查看生成的文档。 如果你想自定义Swagger UI的主题,你可以在application.properties中添加以下配置: ```properties # Swagger UI主题 knife4j.swaggerui.path=/doc.html knife4j.swaggerui.title=Swagger2接口文档 knife4j.swaggerui.description=Swagger2接口文档 knife4j.swaggerui.version=1.0 knife4j.swaggerui.contact.name=联系人姓名 knife4j.swaggerui.contact.email=联系人邮箱 knife4j.swaggerui.contact.url=联系人网址 knife4j.swaggerui.license.name=许可证名称 knife4j.swaggerui.license.url=许可证网址 knife4j.swaggerui.enable=true # 配置主题 knife4j.swaggerui.theme=flattop ``` 这将启用knife4j并使用flattop主题。 希望这些步骤对你有所帮助。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值