0


Springboot3.0.0+集成SpringDoc并配置knife4j的UI

环境:JDK17,Springboot3+,springdoc2+,knife4j 4+

Springdoc本身也是集成了Swagger3,而knife4j美化了Swagger3的UI

Knife4j官网:

快速开始 | Knife4j

Springdoc官网

OpenAPI 3 Library for spring-boot

1.pom配置

由于此knife4j内依赖了SpringDoc,因此不用另外引入springdoc的依赖

        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>knife4j-openapi3-jakarta-spring-boot-starter</artifactId>
            <version>4.3.0</version>
        </dependency>

springdoc依赖(无需引入),亲测引入也不会冲突

        <dependency>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
            <version>2.2.0</version>
        </dependency>

2.application.yml配置

springdoc:
    swagger-ui:
        path: /swagger-ui.html
        tags-sorter: alpha
        operations-sorter: alpha
        enabled: true
    api-docs:
        path: /v3/api-docs
        enabled: true
    group-configs:
          group: platform
          paths-to-match: /**
          packages-to-scan: com.license4j.license

knife4j:
    enable: true
    setting:
        language: zh_cn

3.代码配置

addInterceptors主要放开了文档的路径访问

addResourceHandlers主要设置了接口文档静态资源路径

addResourceHandlers配置很重要,不配置会导致接口文档404,后台也会报异常

No mapping for GET /doc.html

import jakarta.annotation.Resource;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.ArrayList;
import java.util.List;

@Configuration
public class WebMvcRegistrationsConfig extends WebMvcConfigurationSupport {

    @Resource
    private LicenseInterceptor licenseInterceptor;

    public WebMvcRegistrationsConfig(LicenseInterceptor licenseInterceptor) {
        this.licenseInterceptor = licenseInterceptor;
    }

    /**
     * 拦截器配置
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 无需拦截的接口集合
        List<String> ignorePath = new ArrayList<>();
        // swagger
        ignorePath.add("/swagger-resources/**");
        ignorePath.add("/doc.html");
        ignorePath.add("/v3/**");
        ignorePath.add("/webjars/**");
        ignorePath.add("/springdoc/**");
        ignorePath.add("/static/**");
        ignorePath.add("/templates/**");
        ignorePath.add("/error");
        ignorePath.add("/cipher/check");
        ignorePath.add("/manager/login");
        ignorePath.add("/swagger-ui.html");
        //先拦截认证,再拦截授权
        registry.addInterceptor(licenseInterceptor).addPathPatterns("/**").excludePathPatterns(ignorePath);
    }

    /**
     * 静态资源配置
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}

swagger基本配置

import io.swagger.v3.oas.models.ExternalDocumentation;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class SwaggerConfig {
    private Info info(){
        return new Info()
                .title("API接口文档")
                .description("")
                .version("v1.0.0");
    }
    @Bean
    public OpenAPI springShopOpenAPI() {
        return new OpenAPI()
                .info(info())
                .externalDocs(externalDocumentation());
    }
}

4.访问路径 ip+端口+doc.html


本文转载自: https://blog.csdn.net/qq_29519041/article/details/132341932
版权归原作者 Anakki 所有, 如有侵权,请联系我们删除。

“Springboot3.0.0+集成SpringDoc并配置knife4j的UI”的评论:

还没有评论