当前位置:网站首页>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 cannot connect to the intranet database
![November 22, 2021 [reading notes] - bioinformatics and functional genomics (Section 5 of Chapter 5 uses a comparison tool similar to blast to quickly search genomic DNA)](/img/de/7ffcc8d6911c499a9798ac9215c63f.jpg)
November 22, 2021 [reading notes] - bioinformatics and functional genomics (Section 5 of Chapter 5 uses a comparison tool similar to blast to quickly search genomic DNA)

Simple application of generating function -- integer splitting 2
![February 14, 2022 [reading notes] - life science based on deep learning Chapter 2 Introduction to deep learning (Part 1)](/img/ff/e4df5a66cda74ee0d71015b7d1a462.jpg)
February 14, 2022 [reading notes] - life science based on deep learning Chapter 2 Introduction to deep learning (Part 1)

Opencv4.2.0+vs2015 configuration

深度学习——卷积的滑动窗口实现

Sword finger offer II 076 The kth largest number in the array (use heap to solve TOPK problem)

深度学习——Bounding Box预测

深度学习——特征点检测和目标检测
![2022.01.20 [bug note] | qiime2: an error was encoded while running dada2 in R (return code 1)](/img/c0/2c5efdbefd6f9b591541b1204c3ea2.jpg)
2022.01.20 [bug note] | qiime2: an error was encoded while running dada2 in R (return code 1)
随机推荐
Deep learning -- sequence model and mathematical symbols
Development technology sharing of Jingtan NFT digital collection system
Deep learning -- using word embedding and word embedding features
F12 packet capture is used for the whole process analysis of postman interface test
Cesium learning notes (I)
Leetcode47. full arrangement II
AcrelEMS能效管理平台为高层小区用电安全保驾护航
Sword finger offer II 076 The kth largest number in the array (use heap to solve TOPK problem)
Summary and common applications of direction and angle operators in Halcon
【NVMe2.0b 14-8】Set Features(下篇)
More, faster, better and cheaper. Here comes the fastdeploy beta of the low threshold AI deployment tool!
领域驱动下cloud项目中单个服务的示例
深度学习——GRU单元
Deep learning -- language model and sequence generation
Opencv image
December 4, 2021 [metagenome] - sorting out the progress of metagenome process construction
全栈最全性能测试理论-总结
Dlib library blink
C. Fishingprince Plays With Array
December 13, 2021 [reading notes] | understanding of chain specific database building