java springboot 正合Knife4j框架

要将 Knife4j(之前称为 SwaggerBootstrapUi)集成到 Spring Boot 项目中。

可以按照以下步骤操作:

1、添加依赖

在 pom.xml 文件中添加 Knife4j 的相关依赖。确保 Spring Boot 版本与 Knife4j 支持的版本相匹配。

这里以 Maven 为例:

XML 复制代码
<dependencies>
    <!-- Springfox Swagger 依赖 -->
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-boot-starter</artifactId>
        <version>3.0.0</version> <!-- 根据实际需要选择版本 -->
    </dependency>
    
    <!-- Knife4j UI 依赖 -->
    <dependency>
        <groupId>com.github.xiaoymin</groupId>
        <artifactId>knife4j-spring-boot-starter</artifactId>
        <version>3.0.3</version> <!-- 请根据最新版本进行调整 -->
    </dependency>
</dependencies>

2、配置 Swagger 和 Knife4j

在 src\main\resources 目录下创建或修改 application.yml 或 application.properties 文件,配置 Swagger 和 Knife4j。

application.yml 示例配置:

XML 复制代码
springfox:
  documentation:
    swagger-ui:
      enabled: true
    knife4j:
      enable: true # 开启 Knife4j 增强功能

server:
  port: 8080

# 如果使用的是Spring Security,可能需要配置Swagger的访问权限
# security:
#   ignored: /swagger-ui.html**, /webjars/**

3、创建 Swagger 配置类

可选地,可以创建一个 Swagger 配置类来定制 API 文档信息。

以下是一个简单的示例:

java 复制代码
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

@Configuration
@EnableSwagger2WebMvc // 使用Swagger2
public class Knife4jConfig {

    @Bean(value = "defaultApi2")
    public Docket defaultApi2() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(new ApiInfoBuilder()
                        .title("应用名称")
                        .description("API 描述")
                        .termsOfServiceUrl("http://yourwebsite.com")
                        .contact("联系人")
                        .version("1.0")
                        .build())
                // 分组名称
                .groupName("默认")
                .select()
                // 这里指定Controller扫描包路径
                .apis(RequestHandlerSelectors.basePackage("包路径"))
                .paths(PathSelectors.any())
                .build();
    }
}

4、访问 Swagger UI

完成上述配置后,启动 Spring Boot 应用,然后访问 http://localhost:8080/项目名/doc.html(或根据你的端口配置调整),能看到 Knife4j 增强版的 Swagger UI 界面。

相关推荐
微露清风28 分钟前
系统性学习C++-第五讲-内存管理
java·c++·学习
计算机毕业设计木哥32 分钟前
计算机毕业设计选题推荐:基于SpringBoot和Vue的快递物流仓库管理系统【源码+文档+调试】
java·vue.js·spring boot·后端·课程设计
qiuiuiu41333 分钟前
正点原子RK3568学习日志-编译第一个驱动程序helloworld
linux·c语言·开发语言·单片机
2351637 分钟前
【LeetCode】146. LRU 缓存
java·后端·算法·leetcode·链表·缓存·职场和发展
聪明的笨猪猪40 分钟前
Java Redis “运维”面试清单(含超通俗生活案例与深度理解)
java·经验分享·笔记·面试
FIavor.1 小时前
怎么办这是Apifox里执行http://localhost:9002/goods/getByUserName?name=“张三“为什么我改了还是500?
java·网络·网络协议·http
编程饭碗1 小时前
【Java集合】
java
岁岁岁平安1 小时前
Java的双重检查锁机制(DCL)与懒加载的单例模式
java·单例模式·synchronized·
Jabes.yang1 小时前
Java面试场景:从Spring Boot到Kubernetes的技术问答
java· 面试· spring boot· 微服务· kubernetes· 技术栈· redis
molong9311 小时前
Kotlin 内联函数、高阶函数、扩展函数
android·开发语言·kotlin