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

Swagger接口文档使用(三种)

1.使用直接使用springdoc-OpenAPI3:

官网地址

<dependency><groupId>org.springdoc</groupId><artifactId>springdoc-openapi-ui</artifactId><version>1.7.0</version>
</dependency>

application.yml中添加SpringDoc配置 (根据需要配置即可)

springdoc:swagger-ui:path: /swagger.html  #自定义文档访问路径,默认/swagger-ui/index.htmlapi-docs:path: /api-docs  #自定义文档api元数据访问路径,默认访问路径是/v3/api-docsgroup-configs: #接口分组配置- group: 1.APP端业务接口packages-to-scan: com.jungle.app #包路径- group: 2.管理端接口packages-to-scan: com.jungle.admin

更多yml配置可查看官网地址
http://localhost:8080/swagger.html

在这里插入图片描述
在application.yml配置文件中添加自定义配置信息:

# 自定义Swagger配置
swagger:# 是否开启swaggerenabled: true#路径访问前缀contextPath:info:# 标题title: 'jungle——接口文档'# 描述description: '这是描述'# 版本version: '版本号: 2.0.1'# 作者信息contact:name: Jungleemail: example@qq.com

SwaggerProperties配置属性类,读取swagger自定义配置信息

/*** swagger 配置属性** @author Lion Li*/
@Data
@Component
@ConfigurationProperties(prefix = "swagger")
public class SwaggerProperties {/*** 是否开启 openApi 文档*/private Boolean enabled = true;private String contextPath;@NestedConfigurationPropertyprivate InfoProperties info = new InfoProperties();@NestedConfigurationPropertyprivate ExternalDocumentation externalDocs;private List<Tag> tags = null;@NestedConfigurationPropertyprivate Paths paths = null;@NestedConfigurationPropertyprivate Components components = null;/*** <p>* 文档的基础属性信息* </p>** 为了 springboot 自动生产配置提示信息,所以这里复制一个类出来*/@Datapublic static class InfoProperties {private String title = "";private String description = null;@NestedConfigurationPropertyprivate Contact contact = null;@NestedConfigurationPropertyprivate License license = null;private String version = null;}
	//注入SwaggerProperties@Autowiredprivate SwaggerProperties swaggerProperties;//配置类中装配OpenAPI对象,配置文档信息@Bean@ConditionalOnMissingBean(OpenAPI.class)public OpenAPI openApi() {OpenAPI openApi = new OpenAPI();// 文档基本信息SwaggerProperties.InfoProperties infoProperties = swaggerProperties.getInfo();Info info = convertInfo(infoProperties);openApi.info(info);// 扩展文档信息openApi.externalDocs(swaggerProperties.getExternalDocs());openApi.tags(swaggerProperties.getTags());openApi.paths(swaggerProperties.getPaths());openApi.components(swaggerProperties.getComponents());List<SecurityRequirement> list = new ArrayList<>();list.add(new SecurityRequirement().addList("apikey"));openApi.security(list);return openApi;}

配置swagger接口备注来源于Javadoc

pom文件中引入以下依赖:

<!--在运行时读取 Javadoc 注释-->
<dependency><groupId>org.springdoc</groupId><artifactId>springdoc-openapi-javadoc</artifactId><version>1.6.12</version><!--包含依赖 :therapi-runtime-javadoc-->
</dependency>

在pom文件的 build > plugins > plugin > configuration标签中添加:

<annotationProcessorPaths><path><!-- 注释处理器 --><groupId>com.github.therapi</groupId><artifactId>therapi-runtime-javadoc-scribe</artifactId><version>0.15.0</version></path>
</annotationProcessorPaths>

如果同时存在 swagger注解说明 和 Javadoc 注释。将使用swagger注解说明的值。
springdoc文档地址:https://springdoc.org/#getting-started

2.使用knife4j升级版本v4.x

官方文档:https://doc.xiaominfo.com/docs/quick-start

(1)使用knife4j整合OpenAPI2.0(springboot2.x)

pom文件中引入依赖

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

application.yml中添加配置信息(根据需要配置即可)

#knife4j openapi2.0
knife4j:enable: trueopenapi:title: Char智旅description:  'Char智旅——接口文档'email: 3224431318@qq.com concat: Jungle #作者url: https://docs.xiaominfo.comversion: v2.0.1license: Apache 2.0license-url: https://stackoverflow.com/terms-of-service-url: https://stackoverflow.com/group: #配置接口分组信息one:group-name: 1.APP端业务接口api-rule: packageapi-rule-resources:- com.jungle.apptwo:group-name: 2.系统接口api-rule: packageapi-rule-resources:- com.jungle.admin

添加WebMvcConfiguration配置类

@Configuration
@Slf4j
public class WebMvcConfiguration extends WebMvcConfigurationSupport {/*** 设置静态资源映射** @param registry*/public void addResourceHandlers(ResourceHandlerRegistry registry) {log.info("设置静态资源映射");registry.addResourceHandler("/doc.html").addResourceLocations("classpath:META-INF/resources/");registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:META-INF/resources/webjars/");}
}

浏览器访问:http://localhost:8080/doc.html (根据实际情况)

在这里插入图片描述

(2)使用knife4j整合OpenAPI3.0(springboot2.x)

pom文件引入依赖

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

application.yml配置文件内容可完全参考springdoc-openapi的项目说明,Knife4j只提供了增强部分,如果要启用Knife4j的增强功能,可以在配置文件中进行开启,,,knife4j的增强配置,不需要增强可以不配,增强功能可通过官方文档查看

knife4j:enable: truesetting:language: zh_cnwriter-with-default-pretty-printer: true

浏览器访问:

http://localhost:8080/swagger.html

在这里插入图片描述
)

http://www.lryc.cn/news/2420052.html

相关文章:

  • UTC时间、GMT时间、CST时间
  • HTTP知识点总结,这一篇就够了
  • 一、jQuery概述
  • 【Go】超详细Go入门
  • 【VirtualBox】--- 从零开始搭建 Ubuntu系统 超详细
  • OpenCV入门教程(非常详细)从零基础入门到精通,看完这一篇就够了
  • HTML学习笔记(HTML基础篇)
  • 网址中有无 www 区别是什么
  • I tell you 如何下载文件
  • SpringBoot最常用的50个注解(全是干货,干的要死!)
  • Nodejs的详细安装过程与步骤(超详细)
  • 【MobaXterm】--- 登录SSH服务器 持续更新中
  • Nacos 入门教程
  • 再见,Anaconda的安装和配置老大难问题!
  • Pytorch 最全入门介绍,Pytorch入门看这一篇就够了_pytorch csdn
  • 【设计模式】UML建模之包图、组件图和部署图学习笔记
  • IDEA2023使用手册 【持续更新...】
  • Docker-常用命令大全(附命令详解)
  • emplode php|,PHP版单点登陆实现方案
  • 看美国影片必须了解的英文粗语脏话,好不容易整理出来的
  • 酒吧管理系统、酒吧销售系统
  • 3d弹珠小游戏的制作
  • 将Tp-link无线路由器桥接到Dlink无线路由器上
  • Go 语言 tag 的用处?
  • 如何让游戏中的随机因素重新赢得玩家信任
  • 卡巴斯基误报处理
  • 在智博会上大展异彩的忽米网到底有什么厉害之处?
  • Consul 基本概念、内部原理
  • 如何更改html的默认应用,win10如何修改默认应用
  • 人脸识别主要算法原理