当前位置: 首页 > news >正文

2024年1月Java项目开发指南17:自动接口文档配置

Knife4j 文档 :https://doc.xiaominfo.com/

有能力的建议自己去看文档配置,本文仅做参考,因为官方文档会更新,本文不会,以后说不定本文就过时了。

ok,我们继续。虽然本文是2024年1月Java项目开发指南17,但实际上与前面的并没有什么关联(不是基于之前的项目的),这一点需要你知道。

导入依赖:

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

配置(properties):

# springdoc-openapi
springdoc.swagger-ui.path= /swagger-ui.html
springdoc.swagger-ui.tags-sorter= alpha
springdoc.swagger-ui.operations-sorter= alpha
springdoc.api-docs.path= /v3/api-docs
springdoc.group-configs[0].group= 'default'
springdoc.group-configs[0].paths-to-match= '/**'
springdoc.group-configs[0].packages-to-scan= 'com.guaiguailang.harmony.**'

注意:packages-to-scan 的值 要改成符合自己的项目,不要照抄

如果你配置文件不是这种格式,另外一种格式(yml)如下:

# springdoc-openapi项目配置
springdoc:swagger-ui:path: /swagger-ui.htmltags-sorter: alphaoperations-sorter: alphaapi-docs:path: /v3/api-docsgroup-configs:- group: 'default'paths-to-match: '/**'packages-to-scan: com.xiaominfo.knife4j.demo.web
# knife4j的增强配置,不需要增强可以不配
knife4j:enable: truesetting:language: zh_cn

注意:packages-to-scan 的值 要改成符合自己的项目结构信息

就这样就可以了,还可以搞点有意思的,就是运行的时候输出文档地址,效果就像这样:

image

那么怎么做呢,首先配置文件要有:

# Server Info Setting
server.port=8080
server.servlet.context-path=/
server.servlet.session.timeout=3600

然后在启动文件里面写:

package com.guaiguailang.harmony;import com.github.yitter.contract.IIdGenerator;
import com.github.yitter.contract.IdGeneratorOptions;
import com.github.yitter.idgen.YitIdHelper;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;@SpringBootApplication
public class HarmonyLifeServerApplication {public static void main(String[] args) {
//		SpringApplication.run(HarmonyLifeServerApplication.class, args);// 启动 Spring Boot 应用ConfigurableApplicationContext context = SpringApplication.run(HarmonyLifeServerApplication.class, args);// 获取服务器端口int port = context.getEnvironment().getProperty("server.port", Integer.class);// 获取服务器上下文路径,默认为"/"String contextPath = context.getEnvironment().getProperty("server.servlet.context-path", "/");if (contextPath.equals("/")) {contextPath = "";}System.out.println("^_^ 青山埋忠骨,烟巷葬伟杰        ORZ···");System.out.println("工    作    区  :"+work_space+" (请保证工作区全局唯一)");// 输出项目访问地址System.out.println("访   问  地  址 : http://localhost:" + port + contextPath);// 输出 Swagger 文档地址System.out.println("Swagger 文档地址: http://localhost:" + port + contextPath + "/swagger-ui.html");System.out.println("Knife4j 文档地址: http://localhost:" + port + contextPath + "/doc.html");System.out.println("^_^ 仙之巅 傲世间 先有萌狼后有天    ORZ···");}}

完毕


http://www.mrgr.cn/news/33147.html

相关文章:

  • 如何将生物序列tokenization为token?
  • C++ 笔试常用算法模板
  • Python | Leetcode Python题解之第423题从英文中重建数字
  • ESP32-WROOM-32 [ESP连接路由器+TCP Client 透传 + TCP Server数据发送]
  • C++ | Leetcode C++题解之第424题替换后的最长重复字符
  • Linux:login shell和non-login shell以及其配置文件
  • 注册建造师执业工程规模标准(电力工程)
  • Python 数据类型
  • 使用Postman测试MQTT协议接口
  • 【数据结构与算法】LeetCode:哈希表
  • 在 Linux (aarch64) 编译 OpenJDK 8
  • [Redis][List]详细讲解
  • 每天五分钟玩转深度学习pytorch:L1正则化和L2正则化的应用
  • WPF入门教学九 样式与模板
  • Kubeadm安装k8s集群
  • [产品管理-32]:NPDP新产品开发 - 30 - 文化、团队与领导力 - 领导力与团队的可持续发展
  • 2024/9/21 数学20题
  • 电机学习-有感BLDC开环控制(六步换相)
  • l2p论文环境安装(2) 复刻源码低版本环境版
  • docker minio启动命令