当前位置:网站首页>瑞吉外卖笔记——第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注解中,指定一个请求参数的各个方面 |
边栏推荐
- 线程安全问题及关键字synchronized,volatile
- 监管再次重拳出击,后市如何?2021-05-22
- 【Tensorflow】AttributeError: module 'keras.backend' has no attribute 'tf'
- Supervision strikes again, what about the market outlook?2021-05-22
- 存储过程详解
- 智能指针-使用、避坑和实现
- Geoffery Hinton:深度学习的下一个大事件
- 线代:已知一个特征向量快速求另外两个与之正交的特征向量
- 未来的金融服务永远不会停歇,牛市仍将继续 2021-05-28
- SQL函数 TRUNCATE
猜你喜欢
随机推荐
FreeBSD bnxt以太网驱动源码阅读记录三:
监管再次重拳出击,后市如何?2021-05-22
web测试和app测试的区别?
static修饰的函数有什么特点(static可以修饰所有的变量吗)
此次519暴跌的几点感触 2021-05-21
els strip collision deformation judgment
【ONE·Data || Getting Started with Sorting】
C language improvement (3)
RKMPP 在FFmpeg上实现硬编解码
2022-08-02日报:2022年7月最热的10篇AI论文
You can't accept 60% slump, there is no eligible for gain of 6000% in 2021-05-27
deal!It's July 30th!
LeetCode(剑指 Offer)- 53 - II. 0~n-1中缺失的数字
MySQL - ERROR 1045 (28000): Access denied for user ‘root’@‘localhost’ (using password: YES)
【Tensorflow】AttributeError: ‘_TfDeviceCaptureOp‘ object has no attribute ‘_set_device_from_string‘
动态刷新日志级别
stack && queue
Awesome!Alibaba interview reference guide (Songshan version) open source sharing, programmer interview must brush
政策利空对行情没有长期影响,牛市仍将继续 2021-05-19
Image retrieval method based on deep learning!









