Spring knife4j(swager3)Boot3集成
时间:2025-06-24 11:40:02 来源:新华社
【字体:  

⛰️个人主页:     。

�,;#xff1系列专栏a;


《spring boot实战。

目录。

1.前提条件。

2.导向依赖。

3.配置。

4.注意事项。


写在最后。

1.前提条件。

spring已初始化 boot项目的版本是3X,项目可以正常启动。

作者版本为3.2.2.初始化教程:

新版本的idea创建springng Boot项目-CSDN博客。icon-default.png?t=N7T8https://blog.csdn.net/qq_62262918/article/details/135785412?spm=1001.2014.3001.5501.

2.导依赖。

knife4j官网:

Knife4j · 集Swager2和OpenAPI3为一体的增强解决方案. | Knife4j (xiaominfo.com)。icon-default.png?t=N7T8https://doc.xiaominfo.com/。依赖选择:

作者使用的spring boot 3.2.2.最新版本,因此,最新的knife4j依赖于导入 4.4.0。

pom.xml:。

            com.github.xiaoymin            knife4j-openapi3-jakarta-spring-boot-starter            4.4.0        

3.配置。

官方文件(快速开始):

快速开始 | Knife4j (xiaominfo.com)。icon-default.png?t=N7T8官方文件(快速开始):

快速开始 | Knife4j (xiaominfo.com)。

https://doc.xiaominfo.com/docs/quick-start。icon-default.png?t=N7T8官方文件(详细配置)#xff0;:

3.1 增强模式 | Knife4j (xiaominfo.com)。

https://doc.xiaominfo.com/docs/features/enhance。

application.yml:

springdoc: swagger-ui: path: /swagger-ui.html tags-sorter: alpha operations-sorter: alpha api-docs: path: /v3/api-docs group-configs: - group: '蒾酒' paths-to-match: '/**' #生成文档所需的扫包路径,一般为启动类目录 packages-to-scan: com.mijiu#knife4j配置knife4j: #是否启用增强设置 enable: true #打开生产环境屏蔽 production: false #登录认证是否启用 basic: enable: true username: admin password: 123456 setting: language: zh_cn enable-version: true enable-swagger-models: true swagger-model-name: 用户模块。

写界面:

@Tag注释:标记接口类别。

编写接口:

@Tag注释:标记接口类别

@Operation:操作标记接口。

@RestController@Tag(name = "用户界面")@RequestMapping("/user")public class UserController { private final UserService userService; public UserController(UserService userService) { this.userService = userService; } @GetMapping("/list") @Operation(summary = "用户列表") public List test(){ return userService.list(); } @Operation(summary = "你好") @GetMapping("/hello") public Object test2(){ return "hello"; }}。

启动项目:

浏览器输入:http://ip:port/doc.html。

启动项目:

浏览器输入:http://ip:port/doc.html。

接口已被识别。

接下来,作者和其他信息将配置以下接口文档。

config目录下的新配置类别:

import io.swagger.v3.oas.models.OpenAPI;import io.swagger.v3.oas.models.info.Contact;import io.swagger.v3.oas.models.info.Info;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;/** * @author mijiupro */@Configurationpublic class Knife4jConfig { @Bean public OpenAPI springShopOpenApi() { return new OpenAPI() // 接口文档标题 .info(new Info().title("demo") // 接口文档简介 .description("这是基于Knife4j OpenApi3测试接口文件") // 接口文档版本 .version("1.0版本") // 开发人员联系方式 .contact(new Contact().name("蒾酒") .email("000000000@qq.com"))); }}。

重启项目󿀌再次访问。

重启项目󿀌再次访问

每次打开浏览器输入地址访问都不友好。

启动类优化:

@SpringBootApplication@MapperScan("com.mijiu.mapper")@Slf4jpublic class SpringbootTemplateApplication { public static void main(String[] args) { SpringApplication app = new SpringApplication(SpringbootTemplateApplication.class); Environment env = app.run(args).getEnvironment(); app.setBannerMode(Banner.Mode.CONSOLE); logApplicationStartup(env); } private static void logApplicationStartup(Environment env) { String protocol = "http"; if (env.getProperty("server.ssl.key-store") != null) { protocol = "https"; } String serverPort = env.getProperty("server.port"); String contextPath = env.getProperty("server.servlet.context-path"); if (StringUtils.isBlank(contextPath)) { contextPath = "/doc.html"; } else { contextPath = contextPath + "/doc.html"; } String hostAddress = "localhost"; try { hostAddress = InetAddress.getLocalHost().getHostAddress(); } catch (UnknownHostException e) { log.warn("The host name could not be determined, using `localhost` as fallback"); } log.info(""" ---------------------------------------------------------- \t应用程序“{ }”正在运行中... \t接口文档访问 URL: \t本地: \t\t{ }://localhost:{ }{ } \t外部: \t{ }://{ }:{ }{ } \t配置文件: \t{ } ----------------------------------------------------------""", env.getProperty("spring.application.name"), protocol, serverPort, contextPath, protocol, hostAddress, serverPort, contextPath, env.getActiveProfiles()); }}。

效果如图所示:

点击直接跳转。
效果如图所示:icon-default.png?t=N7T8点击直接跳转,完成!

4.注意事项。

如果您的springboot项目配备了拦截器,访问swagger文档界面将被拦截。解决方案见以下文章。解决spring问题 在boot中使用拦截器导致swager文档无法访问-CSDN博客xie。写在最后。项目模板已开源。开源地址:springboot3x-template: 本项目为单体架构spring web后端服务开发模板+oot3x版c;#xff00集成的常用依赖性c;起步功能,工程初始化脚手架适用于中小项目。大大提高了开发效率。 (gitee.com)。https://gitee.com/mi9688-wine/springboot-template。欢迎star讨论任何问题评论区或私信c;欢迎指正。

[责任编辑:百度一下]
检察日报数字报 | 正义网 |
Copyrights©最高人民检察院 All Rights Reserved.