掘金 后端 ( ) • 2024-05-05 17:13

springboot 3

springboot 3 整合接口文档需要使用 knife 4 j 的高版本 官方网址: https://doc.xiaominfo.com/docs/quick-start

  1. 引入依赖
    在pom.xml文件中引入依赖并刷新maven
<dependency>  
	<groupId>com.github.xiaoymin</groupId>  
	<artifactId>knife4j-openapi3-jakarta-spring-boot-starter</artifactId>  
	<version>4.4.0</version>  
</dependency>
  1. 添加部分配置(yml 、properties 配置文件):此处是 yml 格式,如果是 properties 文件的话还需要大家自己转换
    此数必须修改的是 packages-to-scan 这项,这是你想扫描接口的路径,比如你需要扫描 controller 层,那就是 com.*.controller
    下面是我自己的扫描路径,其他的配置看你自己需求进行修改(增强配置可以不加)
# springdoc-openapi项目配置  
springdoc:  
	swagger-ui:  
		path: /swagger-ui.html  
		tags-sorter: alpha  
		operations-sorter: alpha  
	api-docs:  
		path: /v3/api-docs  
	group-configs:  
		- group: 'default'  
		paths-to-match: '/**'  
		packages-to-scan: com.xiaominfo.knife4j.demo.web  #此处的路径就是你需要扫描接口的路径
# knife4j的增强配置,不需要增强可以不配  
knife4j:  
	enable: true  
	setting:  
		language: zh_cn
  1. controller 层的路径注解需要加上 如:RequestMapper ("/login")等 此处没什么可说的,就是 controller 层的接口路径,如果加上注解的话人家也找不到你的接口

    如果大家需要配置 springboot 2 接口文档的话,可以继续往下看

springboot 2

官网:https://doc.xiaominfo.com/v2/documentation/get_start.html

  1. 引入依赖
 <!-- knife4j 接口文档 -->
 <dependency>
     <groupId>com.github.xiaoymin</groupId>
     <artifactId>knife4j-spring-boot-starter</artifactId>
     <version>2.0.7</version>
 </dependency>
  1. 创建配置类 SwaggerConfig(图来源:语雀—沙鱼(作者)侵权删)
  2. 代码
package com.yupi.usercenter.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
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;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

/\*\*

*   @author: shayu
*   @date: 2022/11/20
*   @ClassName: yupao-backend01
*   @Description: 自定义 Swagger 接口文档的配置
    \*/
    @Configuration
    @EnableSwagger2WebMvc
    public class SwaggerConfig {

    @Bean(value = "defaultApi2")
    public Docket defaultApi2() {
    return new Docket(DocumentationType.SWAGGER\_2)
    .apiInfo(apiInfo())
    .select()
    // 这里一定要标注你控制器的位置
    .apis(RequestHandlerSelectors.basePackage("com.*****.*****.controller"))
    .paths(PathSelectors.any())
    .build();
    }

    /\*\*

    *   api 信息
    *   @return
        \*/
        private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
        .title("*****")
        .description("*****")
        .termsOfServiceUrl("*****")
        .contact(new Contact("*****"))
        .version("1.0")
        .build();
        }
        }
  1. yml 配置(不加此配置可能会出错,因为 mvc 之前的默认路径和 swagger 配置的路径不同,所以会报错 Failed to start bean ‘documentationPluginsBootstrapper’)
spring:
 mvc:
   pathmatch:
     matching-strategy: ant_path_matcher

总结: 如此看来 springboot 3 出来之后整合比 springboot 2 方便很多,不需要进行书写配置类,而是直接在 yml 中进行修改,大家要注意你们创建的 springboot 是什么版本。