当前位置:网站首页>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

边栏推荐
- 《canvas》之第5章 文本操作
- Redis哨兵机制原理详解
- Source code of campus secondary market
- Campus errands wechat applet errands students with live new source code
- 你还在用命令看日志?快用 Kibana 吧,一张图胜过千万行日志
- 投资人跌下神坛:半年0出手,转行送外卖
- VeeamBackup&Replication简介
- 【blackduck】jenkins下配置指定的synopsys-detect扫描版本
- Investors fell off the altar: 0 sales in half a year, transferred to business and delivered takeout
- zabbix 5.0如何将esxi6.7添加到监控
猜你喜欢

Thanos store component
一次mysql的.ibd文件过大处理过程记录

部署搭建decentraland流程讲解

Investors fell off the altar: 0 sales in half a year, transferred to business and delivered takeout

Are you still reading the log by command? Use kibana, one picture is better than ten thousand lines of log

节点数据采集和标签信息的远程洪泛传输

精品商城拼团秒杀优惠折扣全功能完美双端自适应对接个人免签网站源码

Build your own website (19)

类模板案例-【数组类封装】

By proxy, by buyout, the wild era of domestic end-to-end travel is waiting for the next "eternal robbery"
随机推荐
NuScenes关于Radar的配置信息
面试突击61:说一下MySQL事务隔离级别?
Redis的事务机制
单项数据流之子组件修改父组件的值
Redis的持久化机制
leetcode:226. Flip binary tree
VQA needs not only pictures, but also external knowledge! University of Washington & Microsoft proposed revive, using gpt-3 and wikidata to help answer questions
VQA不只需要图片,还需要外部知识!华盛顿大学&微软提出提出REVIVE,用GPT-3和Wikidata来辅助回答问题!...
动荡的中介生意,不安的租房人
“死掉”的诺基亚,一年躺赚1500亿
类模板案例-【数组类封装】
微信小程序:(更新)云开发微群人脉
《canvas》之第5章 文本操作
MySQL数据库:drop、truncate、delete的区别
在同花顺上开户安全吗 开户在哪里申请
Redis的五种数据结构的底层实现原理
[network bandwidth] Mbps & Mbps
单端口RAM实现FIFO
建立自己的网站(19)
JUC多线程:线程池的创建及工作原理
https://mvnrepository.com/search?q=springfox