用 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测试请求,来查看代码中配置的信息,以及参数的描述信息。
热门推荐
从食材到科学:小米红枣粥助力胃息肉康复
胡桃木家具全攻略:从选购到保养的完整指南
有肉Succulent & Gift:四季变换的原木风格设计
原木风格家居:打造自然、环保又温馨的理想居所
火把节:彝族文化的千年传承与现代演绎
轻法式装修融入音乐元素,打造浪漫居家氛围
黑熊视力真相:“熊瞎子”称呼的科学解读
粉色头像背后:解读儿童的心理密码
中考地理生物:快速提升成绩的有效方法揭秘
两个体系,双向奔赴:算电协同还有三大挑战待破
电算深度融合 赋能数字经济
中药治疗在慢病管理中展现新优势
多模态药物:中医药治疗重大疾病的新希望
药食同源,中药茶饮成年轻人养生新宠
设计师从零打造品牌的方法,先了解用户才能做出正确的决策
珠宝设计师的岗位内容与技能要求
双鱼座的精神世界竟如此深邃:边界感的神秘力量
从“宏彦”看当代起名新趋势:文化内涵与个性并重
黄姓宝宝取名指南:五行生肖搭配与文化寓意
黄姓男宝起名:5个霸气名字和4个实用建议
环太湖骑行攻略:400公里的江南水乡之旅
中国十大顶级硬菜,品味华夏饮食瑰宝
血糖7.1,离糖尿病还有多远?
空腹血糖7.1,真的高了吗?
十二生肖配对:五行理论解读性格相合之道
蛇兔配对攻略:从性格互补到感情经营
生肖兔配蛇:从挑剔到默契的完美伴侣
蛇兔配对:五行之合与情感启示
蛇兔相冲?专家:理解包容才是爱情关键
春节厦门深度游:鼓浪屿、厦大、方特灯会4日游