【knife4j-spring-boot】Springboot + knife4j-spring-boot 整合swagger脚手架

swagger-boostrap-ui从1.x版本到如今2.x,同时也更改名字Knife4j

在此记录下 knife4j-spring-boot-starter 的整合。

只需要引入knife4j-spring-boot-starter,无需引入其他的swagger包,knife4j-spring-boot-starter已经包含。

官方版本说明

【knife4j-spring-boot】Springboot + knife4j-spring-boot 整合swagger脚手架_第1张图片


    com.github.xiaoymin
    knife4j-spring-boot-starter
    2.0.8

整体pom



    4.0.0

    
        org.springframework.boot
        spring-boot-starter-parent
        
        2.7.2
        
    

    com.example
    demo
    0.0.1-SNAPSHOT
    2023_demo
    2023_demo

    
        11
    

    

        
        
            org.springframework.boot
            spring-boot-starter
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
        

        
            org.springframework.boot
            spring-boot-starter-web
        

        
            com.baomidou
            mybatis-plus-boot-starter
            3.4.1
        

        
            org.springframework.boot
            spring-boot-starter-freemarker
        

        
        
            com.github.xiaoymin
            knife4j-spring-boot-starter
            2.0.8
        
        

        
        
            com.baomidou
            mybatis-plus-generator
            3.4.1
        

        
            org.apache.velocity
            velocity-engine-core
            2.3
        
        

        
        
            com.baomidou
            dynamic-datasource-spring-boot-starter
            3.5.0
        
        

        
        
            org.springframework.boot
            spring-boot-starter-aop
        
        

        
        
            com.alibaba
            druid-spring-boot-starter
            1.1.9
        
        

        
        
            mysql
            mysql-connector-java
            runtime
        
        

        
        
            org.projectlombok
            lombok
            true
        

        
            cn.hutool
            hutool-all
            5.5.1
        

        
            junit
            junit
            4.12
            test
        

        
        
            com.alibaba
            easyexcel
            2.2.6
        

        
            com.google.guava
            guava
            17.0
        

        
            com.alibaba
            fastjson
            1.2.68
        
        

        
        
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
                
                    
                        
                            org.projectlombok
                            lombok
                        
                    
                
            
        
    

    
        
            aliyun-repo
            aliyun
            http://maven.aliyun.com/nexus/content/groups/public/
        

        
            com.e-iceblue
            https://repo.e-iceblue.cn/repository/maven-public/
        
    



包结构

【knife4j-spring-boot】Springboot + knife4j-spring-boot 整合swagger脚手架_第2张图片

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包
     */
    @Bean
    public 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包
     */
    @Bean
    public 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开头的单独分组
     */
    @Bean
    public 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

结果:

【knife4j-spring-boot】Springboot + knife4j-spring-boot 整合swagger脚手架_第3张图片

你可能感兴趣的:(spring,boot,vue.js,后端)