当前位置:网站首页>Swagger2的配置教程
Swagger2的配置教程
2022-06-29 14:11:00 【get-yuan】
首先新建一个springboot项目,然后在pom文件中导入swagger的相关依赖,分别是以下两个:
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
也可以进入maven官网自行下载其他版本的swagger依赖,maven官方地址如下:
点击进入maven官网,搜索springfox-swagger即可找到

导入相关依赖之后,要想使用Swagger,还需要对Swagger进行配置,在项目中建立一个config包,包下建立一个SwaggerConfig配置类来对Swagger进行配置 ,如下:

在SwaggerConfig类上面写上注解@Configuration和@EnableSwagger2,其中@Configuration
表示这是一个配置类;而@EnableSwagger2注解来启用Swagger;此时Swagger已经可以使用了,但是都是较原始的内容显示。

启动项目,如果springboot版本太高就可以会抛出异常,建议降低版本,我后面就是把springboot版本降到2.5.6就可以启动了。启动之后访问http://localhost:8080/swagger-ui.html

再对swagger进行下一步的配置,因为Swagger实例Bean是Docket,所以必须通过配置Docket实例来配置Swaggger。
package com.hu.SpringBoot01.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.ArrayList;
import java.util.List;
@Configuration //说明这是一个配置类
@EnableSwagger2//该注解开启Swagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
//添加head参数配置start
ParameterBuilder tokenPar = new ParameterBuilder();
List<Parameter> pars = new ArrayList<>();
tokenPar.name("Authorization").description("令牌")//请求头,测试token时可以用到
.modelRef(new ModelRef("string"))
.parameterType("header")
.required(false)
.build();
pars.add(tokenPar.build());
return new Docket(DocumentationType.SWAGGER_2)
.groupName("webApi")
.apiInfo(webApiInfo())
.select()
//扫描的路径包,会将包下的所有被@Api标记类的所有方法作为api
.apis(RequestHandlerSelectors.basePackage("com.hu.SpringBoot01.controller"))
.paths(PathSelectors.any())//所有接口
.build()
.globalOperationParameters(pars);
}
private ApiInfo webApiInfo() {
return new ApiInfoBuilder()
.title("新生入学后台管理系统接口文档")
.description("Swagger2后台管理系统在线文档")
.contact(new Contact("胡雁南","localhost:8080/swagger-ui.html","[email protected]"))
.version("2.1")
.build();
}
}
此时swagger就可以正常使用了,访问http://localhost:8080/swagger-ui.html

边栏推荐
- VQA needs not only pictures, but also external knowledge! University of Washington & Microsoft proposed revive, using gpt-3 and wikidata to help answer questions
- 浅析 Istio——可观测性
- 微信小程序:全新獨家雲開發微群人脈
- 《canvas》之第14章 物理动画
- VQA不只需要图片,还需要外部知识!华盛顿大学&微软提出提出REVIVE,用GPT-3和Wikidata来辅助回答问题!...
- 动荡的中介生意,不安的租房人
- go-zero微服务实战系列(七、请求量这么高该如何优化)
- Are you still reading the log by command? Use kibana, one picture is better than ten thousand lines of log
- MySQL数据库:分区Partition
- Redis的数据过期清除策略 与 内存淘汰策略
猜你喜欢
![[important notice] the 2022 series of awards and recommendations of China graphics society were launched](/img/ae/2fe0cf9964e5fd3b18e5f295638d8b.png)
[important notice] the 2022 series of awards and recommendations of China graphics society were launched

Wechat applet: Halloween avatar box generation tool

织梦dedecms资源素材教程下载网站模板源码(带手机移动端)附安装教程

Source code of campus secondary market

【黑马早报】中公教育市值蒸发逾2000亿;新东方直播粉丝破2000万;HM关闭中国首店;万科郁亮称房地产已触底;微信上线“大爆炸”功能...

Transport layer selective ACK

疯狂的数字藏品,下一个造富神话?

台式机主板上保护cpu的盖子安装和拆卸

传输层 选择性确认 SACK

goby全端口扫描
随机推荐
VeeamBackup&Replication简介
Wechat applet: repair collection interface version cloud development expression package
[use of veux developer tools - use of getters]
人不成熟的特征
留给比亚迪的时间还有三年
leetcode:226. Flip binary tree
建立自己的网站(19)
MySQL数据库:drop、truncate、delete的区别
内网穿透(nc)
GWD:基于高斯Wasserstein距离的旋转目标检测 | ICML 2021
类模板案例-【数组类封装】
单端口RAM实现FIFO
Thanos store component
[important notice] the 2022 series of awards and recommendations of China graphics society were launched
Redis为什么这么快?Redis是单线程还是多线程?
TikTok全球短视频霸主地位或被YouTube反超
"Dead" Nokia makes 150billion a year
Navicat连接MySQL8.0的正确方法(亲测有效)
Redis的缓存雪崩、缓存击穿、缓存穿透与缓存预热、缓存降级
Mysql database - general syntax DDL DML DQL DCL
https://mvnrepository.com/search?q=springfox