SpringBoot整合Knife4j

一、前言

Knife4j一直致力于将目前的Ui提供给更多的平台或者别的语言使用而努力,经过这么长时间的发展,Knife4j提供的轻量级聚合中间件终于诞生了,自2.0.8版本开始,Knife4j 提供了knife4j-aggregation-spring-boot-starter组件,该组件是一个基于Spring Boot系统的starter,他提供了以下几种能力: 最轻量级、最简单、最方便的聚合OpenApi规范的中间件 让所有的基于Spring Boot的Web体系拥有了轻松聚合OpenApi的能力 提供4种模式供开发者选择 基于本地静态JSON文件的方式聚合OpenAPI 基于云端HTTP接口的方式聚合 基于Eureka注册中心的方式聚合 基于Nacos注册中心的方式聚合 基于该starter发布了Docker镜像,跨平台与语言让开发者基于此Docker镜像轻松进行聚合OpenAPI规范 完美兼容所有Spring Boot版本,没有兼容性问题 开发者可以彻底放弃基于Zuul、Spring Cloud Gateway等复杂的聚合方式 兼容OpenAPI2规范以及OpenAPI3规范

二、快速开始

2.1、pom.xml引入依赖

        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>swagger-bootstrap-ui</artifactId>
            <version>1.9.6</version>
        </dependency>
        <dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>knife4j-spring-boot-starter</artifactId>
            <version>2.0.4</version>
        </dependency>

2.2、Configuration配置

import com.github.xiaoymin.swaggerbootstrapui.annotations.EnableSwaggerBootstrapUI;
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.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * @author wno5974
 * @create 2023-07-09 17:47
 * @Deprecated Knife4j配置
 */
@Configuration
@EnableSwagger2
@EnableSwaggerBootstrapUI
public class Knife4jConfiguration {

    @Bean(value = "defaultApi2")
    public Docket defaultApi2() {
        Contact contact = new Contact("won5974","http://cjhhh.xyz:9200/doc.html","1502461615@qq.com");
        Docket docket=new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(new ApiInfoBuilder()
                        //.title("swagger-bootstrap-ui-demo RESTful APIs")
                        .description("# swagger-bootstrap-ui-demo RESTful APIs")
                        .termsOfServiceUrl("http://cjhhh.xyz:9200/doc.html")
                        .contact(String.valueOf(contact))
                        .version("1.0")
                        .build())
                //分组名称
                .groupName("2.X版本")
                .select()
                //这里指定Controller扫描包路径
                .apis(RequestHandlerSelectors.basePackage("com.won"))
                .paths(PathSelectors.any())
                .build();
        return docket;
    }

}

2.3、application.properties配置

knife4j.basic.enable=true
knife4j.basic.username=admin
knife4j.basic.password=123456

三、测试

test.png

posted on 2026-08-10 15:35  爱河  阅读(2)  评论(0)    收藏  举报

导航