您的位置:首页 > 汽车 > 新车 > SpringBoot新手快速入门系列教程十一:自动生成API文档,Springboot3.x集成SpringDoc

SpringBoot新手快速入门系列教程十一:自动生成API文档,Springboot3.x集成SpringDoc

2024/9/8 10:39:47 来源:https://blog.csdn.net/liuyuyefz/article/details/140410262  浏览:    关键词:SpringBoot新手快速入门系列教程十一:自动生成API文档,Springboot3.x集成SpringDoc

本次项目我们用Maven来做,最近发现gradle其实很多项目的支持比较差,所以项目还是用Maven来新建项目。对比了市面上的几种API生成第三方库,只有springdoc 是能够按照文档就能部署出来的。

官网:

OpenAPI 3 Library for spring-boot

参考文章:https://zhuanlan.zhihu.com/p/638887405

步骤一:新建项目

步骤二:项目结构以及代码

pom.xml的dependencies添加:

<dependency><groupId>org.springdoc</groupId><artifactId>springdoc-openapi-starter-webmvc-ui</artifactId><version>2.6.0</version>
</dependency>

HelloController代码如下:

package com.yuye.www.hellospringdoc.controller;import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@Tag(name = "公共接口")
@RestController
@RequestMapping("/public")
public class HelloController {@Operation(summary = "hello")@GetMapping("/hello")public String hello() {return "hello";}
}

步骤三:访问

http://localhost:8080/swagger-ui/index.html

步骤四:

值得注意是官网说还要在Application里面配置:其实不配置貌似也能启动

# swagger-ui custom path
springdoc.swagger-ui.path=/swagger-ui.html

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com