swagger-boostrap-ui从1.x版本到如今2.x,同时也更改名字Knife4j
在此记录下 knife4j-spring-boot-starter 的整合。
只需要引入knife4j-spring-boot-starter,无需引入其他的swagger包,knife4j-spring-boot-starter已经包含。
官方版本说明
<dependency><groupId>com.github.xiaoymin</groupId><artifactId>knife4j-spring-boot-starter</artifactId><version>2.0.8</version>
</dependency>
整体pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><!--<version>3.1.3</version>--><version>2.7.2</version><relativePath/></parent><groupId>com.example</groupId><artifactId>demo</artifactId><version>0.0.1-SNAPSHOT</version><name>2023_demo</name><description>2023_demo</description><properties><java.version>11</java.version></properties><dependencies><!-- starter start --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.4.1</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-freemarker</artifactId></dependency><!-- knife swagger --><dependency><groupId>com.github.xiaoymin</groupId><artifactId>knife4j-spring-boot-starter</artifactId><version>2.0.8</version></dependency><!-- starter end --><!-- mybatis-plus-generator start --><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-generator</artifactId><version>3.4.1</version></dependency><dependency><groupId>org.apache.velocity</groupId><artifactId>velocity-engine-core</artifactId><version>2.3</version></dependency><!-- mybatis-plus-generator end --><!-- 动态数据源 start --><dependency><groupId>com.baomidou</groupId><artifactId>dynamic-datasource-spring-boot-starter</artifactId><version>3.5.0</version></dependency><!-- 动态数据源 end --><!-- aop切面 start --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-aop</artifactId></dependency><!-- aop切面 end--><!-- 数据库连接池 start --><dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.1.9</version></dependency><!-- 数据库连接池 end --><!-- mysql start --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency><!-- mysql end --><!-- 工具 start --><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.5.1</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>test</scope></dependency><!-- excel导出 --><dependency><groupId>com.alibaba</groupId><artifactId>easyexcel</artifactId><version>2.2.6</version></dependency><dependency><groupId>com.google.guava</groupId><artifactId>guava</artifactId><version>17.0</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.68</version></dependency><!-- 工具 end --><!-- doc\pdf\excel互转 start --><!--<dependency><groupId>e-iceblue</groupId><artifactId>spire.pdf.free</artifactId><version>5.1.0</version></dependency><dependency><groupId>e-iceblue</groupId><artifactId>spire.doc</artifactId><version>11.8.1</version></dependency>--><!-- doc\pdf\excel互转 end --></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><excludes><exclude><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId></exclude></excludes></configuration></plugin></plugins></build><repositories><repository><id>aliyun-repo</id><name>aliyun</name><url>http://maven.aliyun.com/nexus/content/groups/public/</url></repository><repository><id>com.e-iceblue</id><url>https://repo.e-iceblue.cn/repository/maven-public/</url></repository></repositories></project>
包结构
swagger配置:
package com.example.demo.config;import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
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.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;/*** @author* @description* @since 2023/8/31*/
@Configuration
@EnableKnife4j
public class SwaggerConfig {/*** 不同包情况下 bi包*/@Beanpublic Docket createRestBiApi() {return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).groupName("报表管理").select().apis(RequestHandlerSelectors.basePackage("com.example.demo.bi.controller")).paths(PathSelectors.any()).build();}/*** 不同包情况下 system包*/@Beanpublic Docket createRestSystemApi() {return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).groupName("系统管理").select().apis(RequestHandlerSelectors.basePackage("com.example.demo.system.controller")).paths(PathSelectors.any()).build();}/*** 同包情况下路径不同 同在system包下,book开头的单独分组*/@Beanpublic Docket createRestBookApi() {return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).groupName("图书管理").select().apis(RequestHandlerSelectors.basePackage("com.example.demo.system.controller")).paths(PathSelectors.regex("/book/.*")).build();}private ApiInfo apiInfo() {return new ApiInfoBuilder().title("2023_demo API管理").description("2023_demo API 1.0").build();}}
yml配置(必须)
spring:mvc:pathmatch:matching-strategy: ant_path_matcher
结果: