当前位置:网站首页>瑞吉外卖笔记——第10讲Swagger
瑞吉外卖笔记——第10讲Swagger
2022-08-02 14:01:00 【萧篱衣】
一、介绍
使用Swagger你只需要按照它的规范去定义接口及接口相关的信息,再通过Swagger衍生出来的一系列项目和工具,就可以做到生成各种格式的接口文档,以及在线接口调试页面等等。
官网:https://swagger.io/
knife4j是为Java MVC框架集成Swagger生成Api文档的增强解决方案。
二、使用方式
1. 导入knife4j的maven坐标
<dependency>
<groupId>com.github.xiaoymin</groupId>
<artifactId>knife4j-spring-boot-starter</artifactId>
<version>3.0.2</version>
</dependency>2. 导入knife4j相关配置类(WebMvcConfig)
@Slf4j
@Configuration
@EnableSwagger2
@EnableKnife4j
public class WebMvcConfig extends WebMvcConfigurationSupport {
@Bean
public Docket createRestApi() {
// 文档类型
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.jf.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("瑞吉外卖")
.version("1.0")
.description("瑞吉外卖接口文档")
.build();
}
3. 设置静态资源(WebMvcConfig中的addResourceHandlers方法),否则接口文档页面无法访问
registry.addResourceHandler("doc.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");4. 在LoginCheckFilter中设置不需要处理的请求路径
"/doc.html",
"/webjars/**",
"/swagger-resources",
"/v2/api-docs"
三、Swagger常用注解
| 注解 | 说明 |
|---|---|
| @Api | 用在请求的类上,例如Controller,表示对类的说明 |
| @ApiModel | 用在类上,通常是实体类,表示一个返回响应数据的信息 |
| @ApiModelProperty | 用在属性上,描述响应类的属性 |
| @ApiOperation | 用在请求的方法上,说明方法的用途、作用 |
| @ApilmplicitParams | 用在请求的方法上,表示一组参数说明 |
| @ApilmplicitParam | 用在@ApilmplicitParams注解中,指定一个请求参数的各个方面 |
边栏推荐
猜你喜欢

第二届中国Rust开发者大会(RustChinaConf 2021~2022)线上大会正式开启报名

MySQL - ERROR 1045 (28000): Access denied for user ‘root’@‘localhost’ (using password: YES)

CVE-2020-27986 (Sonarqube sensitive information leak) vulnerability fix

CVE-2020-27986(Sonarqube敏感信息泄漏) 漏洞修复

配置zabbix自动发现和自动注册。

面试SQL语句,学会这些就够了!!!

史上最全!47个“数字化转型”常见术语合集,看完秒懂~

软件测试和硬件测试的区别及概念

Configure zabbix auto-discovery and auto-registration.

劲爆!阿里巴巴面试参考指南(嵩山版)开源分享,程序员面试必刷
随机推荐
理解TCP长连接(Keepalive)
Kunpeng devkit & boostkit
els 长条碰撞变形判断
标量替换、栈上分配、同步消除
Sentinel源码(三)slot解析
WiFi Association & Omnipeek Packet Capture Analysis
机器学习——交叉验证法
苹果,与Web3 “八字不合”
AWVS工具介绍[通俗易懂]
LeetCode(剑指 Offer)- 53 - II. 0~n-1中缺失的数字
A number of embassies and consulates abroad have issued reminders about travel to China, personal and property safety
打破文件锁限制,以存储力量助力企业增长新动力
史上最全!47个“数字化转型”常见术语合集,看完秒懂~
电脑死机,Word忘了保存怎么办?怎么恢复?(编辑器是WPS)
Large and comprehensive pom file example
玉溪卷烟厂通过正确选择时序数据库 轻松应对超万亿行数据
What is the difference between web testing and app testing?
The world's largest Apache open source foundation is how it works?
云片网案例
【Tensorflow】AttributeError: '_TfDeviceCaptureOp' object has no attribute '_set_device_from_string'