引入依赖
<properties>
<swagger.version>2.9.2</swagger.version>
<swagger-ui.version>2.9.2</swagger-ui.version>
<swagger-annotation-models-version>1.5.21</swagger-annotation-models-version>
</properties>
<dependencies>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>${swagger-annotation-models-version}</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-models</artifactId>
<version>${swagger-annotation-models-version}</version>
</dependency>
<!-- 引入swagger -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger.version}</version>
<exclusions>
<exclusion>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
</exclusion>
<exclusion>
<groupId>io.swagger</groupId>
<artifactId>swagger-models</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- 引入swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger-ui.version}</version>
</dependency>
</dependencies>
核心配置类
package com.ddf.boot.common.core.config;
import java.util.ArrayList;
import java.util.List;
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.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
/**
* swagger的配置类
* <p>
* 项目启动后文档访问地址为[ip]:[port]/[context-path]/swagger-ui.html
* <p>
* _ooOoo_
* o8888888o
* 88" . "88
* (| -_- |)
* O\ = /O
* ___/`---'\____
* . ' \\| |// `.
* / \\||| : |||// \
* / _||||| -:- |||||- \
* | | \\\ - /// | |
* | \_| ''\---/'' | |
* \ .-\__ `-` ___/-. /
* ___`. .' /--.--\ `. . __
* ."" '< `.___\_<|>_/___.' >'"".
* | | : `- \`.;`\ _ /`;.`/ - ` : | |
* \ \ `-. \_ __\ /__ _/ .-` / /
* ======`-.____`-.___\_____/___.-`____.-'======
* `=---='
* .............................................
* 佛曰:bug泛滥,我已瘫痪!
*
* @author dongfang.ding
* @date 2019/5/28 14:09
*/
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket createRestApi() {
ParameterBuilder authorizationPar = new ParameterBuilder();
List<Parameter> pars = new ArrayList<>();
// 添加自定义请求头,这样在生成swagger文档的时候,每次调试都会有这几个请求头
pars.add(authorizationPar.name("Authorization")
.description("token信息")
.modelRef(new ModelRef("string"))
// 自定义请求头名称
.parameterType("header")
// 请求头的默认值
.defaultValue("Bearer ")
// 是否必传
.required(false)
.build());
// 还是添加请求头,将请求方式默认为application/json
pars.add(authorizationPar.name("Content-Type")
.description("Content-Type")
.modelRef(new ModelRef("string"))
.parameterType("header")
.defaultValue("application/json")
.required(true)
.build());
return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo())
.select()
// 在这里配置需要扫描的项目的包的路径
.apis(RequestHandlerSelectors.basePackage("com"))
.paths(PathSelectors.any())
.build()
.globalOperationParameters(pars);
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder().title("spring-boot的脚手架工程").description(
"使用spring-boot集成一些常用框架,来完成一个基本框架的搭建,可以用来作为实际项目开发的基准").version("1.0").build();
}
}
这里只说配置,具体使用相关的 注解这里就不写了,网上有更详细的文档,写这篇的目的是因为swagger之前使用的时候一些包的依赖有问题,需要排除之后重新升级依赖包,当然具体原因及不得了,这里分享在这里。还有就是生成的文档可以自定义添加请求头,这个还是很方便的,配置方法也分享在上面。
当然这里还是要推荐另外一个在线文档yapi.
具体特性可以参考官方,避免了swagger大量的侵入注解,在使用的时候只要使用标准的javadoc
注释,在配合一些上传插件, 就可以做到一键上传,且在线调试功能也比swagger丰富很多。
当然之前也分享过一个这方面的文档,里面说明了如何去搭建以及去配置一键上传接口等方面的内容。搭建自己的服务器环境之安装项目文档工具YApi