用 Swagger 实现接口文档
创作时间:
作者:
@小白创作中心
用 Swagger 实现接口文档
引用
CSDN
1.
https://blog.csdn.net/be_racle/article/details/140413932
Swagger是常用的API文档工具,特别是在微服务架构中。本文将详细介绍如何在Spring Boot项目中集成Swagger,包括添加依赖、创建配置类以及使用Swagger注解来丰富接口文档。
用 Swagger 实现接口文档
1.配置 Swagger
1.1 添加 Swagger 依赖
在pom.xml文件中添加Swagger依赖:
<!--Swagger依赖-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!--Swagger-UI依赖 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
1.2 创建 Swagger 配置类
创建Swagger配置类,完成相关配置项:
package com.example.demo.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
/**
* Swagger 配置类
* 在与 Spring Boot 集成时,放在与 Application.java 同级的目录下
*/
@Configuration
@EnableSwagger2
public class SwaggerConfig {
/**
* 创建 API 应用
* 本例采用指定扫描的包路径来定义指定要建立 API 的目录
*/
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))
.paths(PathSelectors.any())
.build();
}
/**
* 创建该 API 的基本信息(这些基本信息会展现在文档页面中)
* 访问地址:http://项目实际地址/swagger-ui.html
*/
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title(" RESTful APIs")
.description("RESTful APIs")
.termsOfServiceUrl("http://localhost:8080/")
.contact("pipi")
.version("1.0")
.build();
}
}
@Configuration:让Spring加载该类配置。@EnableSwagger2:启用Swagger2。createRestApi函数创建Docket的Bean。apiInfo():用来展示该API的基本信息。select():返回一个ApiSelectorBuilder实例,用来控制哪些接口暴露给Swagger来展现。apis(RequestHandlerSelectors.basePackage()):配置包扫描路径。Swagger会扫描包下所有Controller定义的API,并产生文档内容。如果不想产生API,则使用注解@ApiIgnore。
2.编写接口文档
在完成上述配置后,即生成了文档,但是这样生成的文档主要针对请求本身,而描述自动根据方法等命名产生,对用户并不友好。所以,通常需要自己增加一些说明以丰富文档内容。可以通过以下注解来增加说明:
@Api:描述类/接口的主要用途。@ApiOperation:描述方法用途,给API增加说明。@ApiImplicitParam:描述方法的参数,给参数增加说明。@ApiImplicitParams:描述方法的参数(Multi-Params),给参数增加说明。@ApiIgnore:忽略某类/方法/参数的文档。
具体使用方法:
package com.example.demo.controller;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;
@RestController
public class HelloWorldController {
@ApiOperation(value = "hello", notes = "notes")
@RequestMapping("/hello")
public String hello() throws Exception {
return "HelloWorld ,Spring Boot!";
}
// 使用该注解忽略这个 API
@ApiIgnore
@RequestMapping(value = "/ignoreApi")
public String ignoreApi() {
return "HelloWorld ,Spring Boot!";
}
}
完成上述代码后,启动项目,访问http://localhost:8080/swagger-ui.html就能看到所展示的RESTful API的页面,可以通过单击具体的API测试请求,来查看代码中配置的信息,以及参数的描述信息。
热门推荐
带娃打卡常州恐龙园,亲子游新宠!
打卡蒋巷村:常州最美乡村游
常州Citywalk:古韵今风的完美融合
职场人必知:这些成语形容你最准!
西安钟楼修缮后重新开放!
西安钟楼最佳打卡点揭秘!
遇到奥客怎么办?学习应对奥客技巧,教你优雅处理,提升服务品质
虚拟现实技术在心理治疗中的应用
金银花的功效与搭配:清热解毒,养生保健
菊花泡水喝的功效有哪些
巫行云的爱恨情仇:从电影到原著的解读
天山童姥的复仇之路:爱恨交织下的悲剧人生
如何为你的小说男主选一个霸气侧漏的姓?
乾为天卦:婚姻幸福的古老智慧
嵌入式系统低功耗定时器的实现与优化
户口性质分为哪几种?办理流程详解
杨冰阳教你放下执念,重拾自我
放下执念,拥抱快乐生活
2025年,用正念冥想放下执念
大腿后侧麻木刺痛怎么治疗
以虎皮鱼为宠物的养殖方法(从饲养环境到饲料管理,让你成为虎皮鱼的贴心管家)
绿萝种植指南:水培与土培的优缺点及具体操作
绿萝的生长温度与适宜气温(探究绿萝生长的最佳环境)
念珠菌饮食:饮食指南及需要避免的食物
泻肝安神丸可以经常使用吗
长颈鹿的脖子为啥这么长?
在沙漠中遇到渴死的骆驼,为什么不能碰?碰了后果很严重吗?
鸡腿菇的食用方法
照顾老年期的爱犬!5大注意事项
空气呼吸器气瓶定期检测