<!-- swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<!--swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
spring:
mvc:
pathmatch:
matching-strategy: ant_path_matcher
/**
* @project
* @Description
* @Author songwp
* @Date 2022/8/19 16:01
* @Version 1.0.0
**/
@Configuration
//配置类
@EnableSwagger2
//开启swagger2的自动配置
public class SwaggerConfig {
private static final String TOKEN_HERDER_KEY = "token";
/**
* swagger文档配置
*/
@Bean
public Docket customDocket() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.songwp.controller"))
.paths(PathSelectors.any())
.build()
.globalOperationParameters(this.getParameterList());// 全局配置
}
/**
* 添加head参数配置
*/
private List<Parameter> getParameterList() {
ParameterBuilder clientIdTicket = new ParameterBuilder();
List<Parameter> pars = new ArrayList<Parameter>();
clientIdTicket.name(TOKEN_HERDER_KEY).description("token令牌")
.modelRef(new ModelRef("string"))
.parameterType("header")
.required(false).build(); //设置false,表示clientId参数 非必填,可传可不传!
pars.add(clientIdTicket.build());
return pars;
}
/**
* api相关配置
*/
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("swagger 接口文档")
.contact(new Contact("songwp", "http://localhost:8080/swagger-ui.html", ""))
.description("swagger-bootstrap-ui")
.termsOfServiceUrl("http://localhost:8080/swagger-ui.html")
.version("1.0")
.build();
}
//在Controller层的应用:
//1.在类上应用的注解:
@Api(tags = "这是一个控制器类")
//2.在具体请求方法上的注解:
@ApiOperation(value = "功能总述" , notes = "具体描述")
@ApiParam(value = "请求参数说明")
//在POJO层的应用:
//1.在类上应用的注解:
@ApiModel(description = "XX实体类")
//2.在实体类属性上应用的注解:
@ApiModelProperty(value = "属性说明")
Swagger版本为2.9.2:
直接访问:localhost:8080/swagger-ui.html
Swagger版本为3.0.0:
直接访问:localhost:8080/swagger-ui/index.html