springboot学习(七十三) springboot中使用springdoc替换swagger(springfox)
文章目录
- 前言
- 一、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)相关推荐
- SpringBoot学习笔记(9)----SpringBoot中使用关系型数据库以及事务处理
在实际的运用开发中,跟数据库之间的交互是必不可少的,SpringBoot也提供了两种跟数据库交互的方式. 1. 使用JdbcTemplate 在SpringBoot中提供了JdbcTemplate模板 ...
- SpringBoot学习笔记(4)----SpringBoot中freemarker、thymeleaf的使用
1. freemarker引擎的使用 如果你使用的是idea或者eclipse中安装了sts插件,那么在新建项目时就可以直接指定试图模板 如图: 勾选freeMarker,此时springboot项目 ...
- 【Springboot学习笔记】SpringBoot+Mybatis+Thymeleaf+Layui数据表单从零开始实现按条件模糊分页查询的方法
[Springboot学习笔记]SpringBoot+Mybatis+Thymeleaf+Layui数据表单从零开始实现按条件模糊分页查询的方法 目录 1.搭建环境 1.1直接从网上下载SpringB ...
- SpringBoot学习笔记(16)----SpringBoot整合Swagger2
Swagger 是一个规范和完整的框架,用于生成,描述,调用和可视化RESTful风格的web服务 http://swagger.io Springfox的前身是swagger-springmvc,是 ...
- python--Flask学习(七)--利用Flask中的werkzeug.security模块加密
若将密码以明文的方式保存在数据库中是不安全的,可以使用一些如MD5的方式加密,但像这种加密方式也是存在安全隐患的,这里我们来学习一下利用Flask中的werkzeug.security模块加密. 1. ...
- springboot学习(七十一)解决问题:the URL contained a potentially malicious String “;“
访问某个请求报错: org.springframework.security.web.firewall.RequestRejectedException: The request was reject ...
- 《spring-boot学习》-05-spring boot中redis应用
转载http://www.cnblogs.com/ityouknow/p/5748830.html
- springboot学习笔记-5 springboot整合shiro
shiro是一个权限框架,具体的使用可以查看其官网 http://shiro.apache.org/ 它提供了很方便的权限认证和登录的功能. 而springboot作为一个开源框架,必然提供了和sh ...
- springboot学习(六十七) springboot项目通过gradle-docker-plugin插件构建为doker镜像并推送至镜像私服
文章目录 前言 1.使用gradle部署springboot项目 2.Docker开启远程访问 3.安装镜像私服Harbor 4.gradle中配置插件 前言 springboot从2.4默认提供了打 ...
最新文章
- Linux下gdb调试工具的使用
- Hadoop2.2.0集群在RHEL6.2下的安装实战
- 你以为大厂的代码就不烂?看看这几个公众号怎么说!
- C++ 数据结构,vector与栈介绍
- [CSS学习] line-height属性讲解
- 动态规划实战16 leetcode-198. House Robber
- Linux 安装Python3
- 数字图像处理第三版4.8.4例子GLPF高斯低通滤波器matlab程序
- GAMES101-现代计算机图形学入门-闫令琪 - lecture7 着色(Shading) - 课后笔记
- mysql端口establish_establish_connection使用方法
- 自己制作的直流电机驱动器
- !和!!的区别和用法
- C语言编译发现注释错误,在对C语言程序进行编译时,可以发现注释行中的拼写错误。...
- CSDN邀您加入GitChat,让知识变现!
- 【HTML + CSS】如何引入icon图标
- 医美“轻”触网,“直播+视频面诊”能为美团医美乘风破浪吗?
- ubuntu下android开发安装手机驱动
- 内容付费时代,你愿意为文章付费吗?
- 5个NFT趋势将使社交媒体受众进入web3
- 网页版简历制作经验分享