文章目录

  • 前言
  • 一、springdoc介绍
  • 二、使用步骤
    • 1.引入库
    • 2. 创建一个spring配置类,添加springdoc的配置
    • 3. 常用的swagger注解和springdoc的对应关系
    • 4. 一个接口类的示例
    • 5. 配置文件配置
    • 6. WebMvc配置
    • 7. UI

前言

距离swagger上次发布版本已经过去两年多了,一直没有更新,与当前的springboot2.6.x、springboot2.7.x存在各种兼容问题,对于即将发布的springboot3.x,可能存在更多兼容问题。如下图所示。

其实swagger还在更新,应该是springfox不更新导致的,所以需要使用其他的API管理工具代替,springdoc是一种选择


一、springdoc介绍

SpringDoc是一款可以结合SpringBoot使用的API文档生成工具,基于OpenAPI 3,是一款更好用的Swagger库!值得一提的是SpringDoc不仅支持Spring WebMvc项目,还可以支持Spring WebFlux项目,甚至Spring Rest和Spring Native项目。

二、使用步骤

1.引入库

gradle:

api group: 'org.springdoc', name: 'springdoc-openapi-ui', version: '1.6.11'

maven:

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

2. 创建一个spring配置类,添加springdoc的配置

@AutoConfiguration
public class SpringDocConfig {@Beanpublic OpenAPI openAPI() {return new OpenAPI().info(new Info().title("newframe-接口文档").description("基于SpringDoc的在线接口文档").version("0.0.1"));}@Beanpublic GroupedOpenApi publicApi() {return GroupedOpenApi.builder().group("权限相关").packagesToScan("com.iscas.biz.controller.common.auth").build();}@Beanpublic GroupedOpenApi adminApi() {return GroupedOpenApi.builder().group("默认").pathsToMatch("/**").build();}}

3. 常用的swagger注解和springdoc的对应关系

4. 一个接口类的示例

@Tag(name = "组织机构管理-OrgController")
@RestController
@RequestMapping("/org")
@Validated
@ConditionalOnMybatis
public class OrgController extends BaseController {private final OrgService orgService;public OrgController(OrgService orgService) {this.orgService = orgService;}@Operation(summary="[组织机构]获取组织机构树", description="create by:朱全文 2021-02-20")@GetMappingpublic TreeResponse get() throws BaseException {return getTreeResponse().setValue(orgService.getTree());}@Operation(summary="[组织机构]新增组织机构节点", description="create by:朱全文 2021-02-20")@io.swagger.v3.oas.annotations.parameters.RequestBody(required = true, description = "组织机构数据",content = @Content(schema = @Schema(implementation = Org.class)))@PostMapping("/node")public ResponseEntity addNode(@Valid @RequestBody Org org) throws BaseException {return getResponse().setValue(orgService.addOrg(org));}@Operation(summary="[组织机构]修改组织机构节点", description="create by:朱全文 2021-02-20")@io.swagger.v3.oas.annotations.parameters.RequestBody(required = true, description = "组织机构数据",content = @Content(schema = @Schema(implementation = Org.class)))@PutMapping("/node")public ResponseEntity editNode(@Valid @RequestBody Org org) {return getResponse().setValue(orgService.editOrg(org));}@Operation(summary="[组织机构]删除组织机构节点", description="create by:朱全文 2021-02-20")@io.swagger.v3.oas.annotations.parameters.RequestBody(required = true, description = "组织机构ID集合", content = @Content(examples = @ExampleObject(value = "[123, 124]")))@PostMapping("/node/del")@Caching(evict = {@CacheEvict(value = "auth", key = "'url_map'"),@CacheEvict(value = "auth", key = "'menus'"),@CacheEvict(value = "auth", key = "'role_map'")})public ResponseEntity deleteNode(@RequestBody List<Integer> orgIds) {AssertCollectionUtils.assertCollectionNotEmpty(orgIds, "orgIds不能未空");orgService.deleteNode(orgIds);return getResponse();}}

5. 配置文件配置

springdoc.swagger-ui.doc-expansion=none
springdoc.swagger-ui.path=/doc.html

还有其他的各种配置,可以在写配置的时候查看提示

6. WebMvc配置

@AutoConfiguration
public class WebMvcConfig implements WebMvcConfigurer {@Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) {registry.addResourceHandler("/swagger-ui/**").addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/").resourceChain(false);registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");}@Overridepublic void addViewControllers(ViewControllerRegistry registry) {registry.addViewController("/swagger-ui/").setViewName("forward:/swagger-ui/index.html");}
}

7. UI

访问地址:http://localhost:7901/demo/swagger-ui/ 或 http://localhost:7901/demo/doc.html
UI还使用swagger的UI,如下图所示:

springboot学习(七十三) springboot中使用springdoc替换swagger(springfox)相关推荐

  1. SpringBoot学习笔记(9)----SpringBoot中使用关系型数据库以及事务处理

    在实际的运用开发中,跟数据库之间的交互是必不可少的,SpringBoot也提供了两种跟数据库交互的方式. 1. 使用JdbcTemplate 在SpringBoot中提供了JdbcTemplate模板 ...

  2. SpringBoot学习笔记(4)----SpringBoot中freemarker、thymeleaf的使用

    1. freemarker引擎的使用 如果你使用的是idea或者eclipse中安装了sts插件,那么在新建项目时就可以直接指定试图模板 如图: 勾选freeMarker,此时springboot项目 ...

  3. 【Springboot学习笔记】SpringBoot+Mybatis+Thymeleaf+Layui数据表单从零开始实现按条件模糊分页查询的方法

    [Springboot学习笔记]SpringBoot+Mybatis+Thymeleaf+Layui数据表单从零开始实现按条件模糊分页查询的方法 目录 1.搭建环境 1.1直接从网上下载SpringB ...

  4. SpringBoot学习笔记(16)----SpringBoot整合Swagger2

    Swagger 是一个规范和完整的框架,用于生成,描述,调用和可视化RESTful风格的web服务 http://swagger.io Springfox的前身是swagger-springmvc,是 ...

  5. python--Flask学习(七)--利用Flask中的werkzeug.security模块加密

    若将密码以明文的方式保存在数据库中是不安全的,可以使用一些如MD5的方式加密,但像这种加密方式也是存在安全隐患的,这里我们来学习一下利用Flask中的werkzeug.security模块加密. 1. ...

  6. springboot学习(七十一)解决问题:the URL contained a potentially malicious String “;“

    访问某个请求报错: org.springframework.security.web.firewall.RequestRejectedException: The request was reject ...

  7. 《spring-boot学习》-05-spring boot中redis应用

    转载http://www.cnblogs.com/ityouknow/p/5748830.html

  8. springboot学习笔记-5 springboot整合shiro

    shiro是一个权限框架,具体的使用可以查看其官网 http://shiro.apache.org/  它提供了很方便的权限认证和登录的功能. 而springboot作为一个开源框架,必然提供了和sh ...

  9. springboot学习(六十七) springboot项目通过gradle-docker-plugin插件构建为doker镜像并推送至镜像私服

    文章目录 前言 1.使用gradle部署springboot项目 2.Docker开启远程访问 3.安装镜像私服Harbor 4.gradle中配置插件 前言 springboot从2.4默认提供了打 ...

最新文章

  1. Linux下gdb调试工具的使用
  2. Hadoop2.2.0集群在RHEL6.2下的安装实战
  3. 你以为大厂的代码就不烂?看看这几个公众号怎么说!
  4. C++ 数据结构,vector与栈介绍
  5. [CSS学习] line-height属性讲解
  6. 动态规划实战16 leetcode-198. House Robber
  7. Linux 安装Python3
  8. 数字图像处理第三版4.8.4例子GLPF高斯低通滤波器matlab程序
  9. GAMES101-现代计算机图形学入门-闫令琪 - lecture7 着色(Shading) - 课后笔记
  10. mysql端口establish_establish_connection使用方法
  11. 自己制作的直流电机驱动器
  12. !和!!的区别和用法
  13. C语言编译发现注释错误,在对C语言程序进行编译时,可以发现注释行中的拼写错误。...
  14. CSDN邀您加入GitChat,让知识变现!
  15. 【HTML + CSS】如何引入icon图标
  16. 医美“轻”触网,“直播+视频面诊”能为美团医美乘风破浪吗?
  17. ubuntu下android开发安装手机驱动
  18. 内容付费时代,你愿意为文章付费吗?
  19. 5个NFT趋势将使社交媒体受众进入web3
  20. 网页版简历制作经验分享

热门文章

  1. 对“佩戴低度数近视镜可延缓近视发展?”的一些疑问
  2. HTTP必须知道的几种状态码
  3. 【动态规划】多重背包问题
  4. 导图解房(01)黄金圈法则解读 买房这件事儿
  5. Win10安装Kali子系统
  6. [C/C++]#ifndef,#define用法
  7. CAD带文字线型的文字偏移bug介绍
  8. python必备常用英语词汇
  9. 上楼梯的走法 ← 递归
  10. odoo11在win10环境搭建