【SpringBoot深入浅出系列】SpringBoot集成swagger2

网友投稿 744 2022-05-30

一、创建 Springboot 项目

步骤见https://blog.csdn.net/u012069313/article/details/122319305

二、添加Maven依赖

swagger2.9及以下版本添加如何依赖:

io.springfox springfox-swagger2 io.springfox springfox-swagger-ui

swagger2.10开始支持两种模式:WebFlux,WebMVC,添加以下依赖(以WebMVC为例):

【SpringBoot深入浅出系列】SpringBoot集成swagger2

io.springfox springfox-swagger2 2.10.5 io.springfox springfox-swagger-ui 2.10.5 io.springfox springfox-spring-webmvc 2.10.5

三、application.yml 中添加配置

swagger: title: swagger-test description: swagger-test version: 1.0 terms-of-service-url: http://127.0.0.1:8080 contact: name: swagger-test url: http://127.0.0.1:8080 email: test@chaoyue.com

四、添加配置类

@EnableSwagger2WebMvc //swagger2.9及以下版本为@EnableSwagger2 @Configuration public class Swagger2Config { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.chaoyue.swagger2test.controller")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("api接口文档") .contact(new Contact("test","http://127.0.0.1:8080","test@chaoyue.com")) .version("1.0") .description("api描述") .build(); } }

五、Controller中添加注解

@Api("HelloController") @RestController public class HelloController { @GetMapping(value = "/user") public String getUser() { return "user"; } }

六、测试

输入测试地址:http://localhost:8080/swagger-ui.html

Spring Boot

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:AE 动效工作流技巧 —— 减少 Bodymovin 导出的 JSON 大小并提升性能(一)
下一篇:f1tenth案例学习与调试ros1版本pc端
相关文章