当前位置:网站首页>swagger使用
swagger使用
2022-06-30 08:04:00 【sashaSloan】
swagger使用
(新手笔记,狂神搬运工)
**本篇依据狂神瑞吉外卖项目优化部分进行记录
介绍
使用Swagger你只需要按照它的规范去定义接口及接口相关的信息,再通过Swagger衍生出来的一系列项目和工具,就可以做到生成各种格式的接口文档,以及在线接口调试页面等等。
官网: https:/ /swagger.io/
knife4j是为Java MVC框架集成Swagger生成Api文档的增强解决方案。
swagger使用
swagger配置
1、导入maven坐标
<dependency>
<groupld>com.github.xiaoymin</groupld>
<artifactld>knife4j-spring-boot-starter</artifactld>
<version>3.0.2</version>
</dependency>
2、导入knife4j相关配置类(WebMvcConfig)
在mvc配置文件WebMvcConfig中开启swagger和knife4j
@EnableSwagger2
@EnableKnife4j
public class WebMvcConfig extends WebMvcConfigurationSupport {
......
//在类里面创建下面两个方法
@Bean
public Docket createRestApi()
{
//文档类型
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.itheima.reggie.controller"))
.paths(PathSelectors.any())
.build();
}
private ApiInfo apiInfo(){
return new ApiInfoBuilder()
.title("瑞吉外卖"). version("1.0")
. description("瑞吉外卖接口文档").build() ;
}
3、设置静态资源(否则接口文档页面无法访问)
设置静态资源映射(WebMvcConfig类中的addResourceHandlers方法),否则接口文档页面无法访问。
// 解决swagger无法访问
registry.addResourceHandler("/swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
// 解决swagger的js文件无法访问
registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
4、在LoginCheckFilter中设置不需要处理的请求路径
访问localhost:8080/doc.html
在这里就可以看到接口信息和进行接口测试了。
swagger常用注解
在类和方法上加上相关注解进行配置


边栏推荐
- MySQL quotation sentence is unlocked: algorithm=insert, lock=none
- min_ max_ Gray operator understanding
- Experiment 3 remote control
- Simple application of generating function -- integer splitting 2
- January 23, 2022 [reading notes] - bioinformatics and functional genomics (Chapter 6: multiple sequence alignment)
- JS代码案例
- 安科瑞高等学校校园建筑节能监管系统建设
- 【NVMe2.0b 14-5】Firmware Download/Commit command
- Deep learning - brnn and DRNN
- Cesium learning notes (I)
猜你喜欢

CRM能为企业带来哪些管理提升
![[flower carving experience] 13 build the platformio ide development environment of esp32c3](/img/32/2c30afe77bf82774479a671ff16898.jpg)
[flower carving experience] 13 build the platformio ide development environment of esp32c3

【NVMe2.0b 14】NVMe Admin Command Set

Cesium learning notes (III) creating instances

冰冰学习笔记:快速排序

小程序使用二维码插件

深度学习——目标定位

Transformer architecture understanding
![[JUC series] overview of fork/join framework](/img/49/2cadac7818b0c1100539fd32ec0d37.png)
[JUC series] overview of fork/join framework

Simple application of generating function
随机推荐
深度学习——使用词嵌入and词嵌入特征
【Tensorflow-gpu】window11下深度学习环境搭建
CRM&PM如何帮助企业创造最优销售绩效
【NVMe2.0b 14-3】Doorbell Buffer Config command、Device Self-test command
深度学习——嵌入矩阵and学习词嵌入andWord2Vec
【NVMe2.0b 14-5】Firmware Download/Commit command
CRM能为企业带来哪些管理提升
Simple application of generating function -- integer splitting 2
Cesium learning notes (I)
ACM. HJ48 从单向链表中删除指定值的节点 ●●
tp5设置直接下载文件
Game 280 problem2: minimum operands to turn an array into an alternating array
Cesium learning notes (II) uploading data using rest API
QT event cycle
Want to change careers, but don't know what to do? This article is written for you who are confused
Leetcode47. full arrangement II
深度学习——残差网络ResNets
AcrelEMS能效管理平台为高层小区用电安全保驾护航
微信小程序使用vant weapp报错
December 4, 2021 [metagenome] - sorting out the progress of metagenome process construction