一、代码
@Configuration
@EnableOpenApi
@EnableWebMvc
public class MySwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.OAS_30)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.exam.manage"))
.paths(PathSelectors.any())
.build()
.securitySchemes(Collections.singletonList(securityScheme()))
.securityContexts(Collections.singletonList(securityContext()));
}
private SecurityScheme securityScheme() {
//return new ApiKey("Authorization", "Authorization", "header");
return new ApiKey("X-Token", "X-Token", "header");
}
private SecurityContext securityContext() {
return SecurityContext.builder()
.securityReferences(defaultAuth())
.forPaths(PathSelectors.regex("^(?!auth).*$"))
.build();
}
private List<SecurityReference> defaultAuth() {
AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
authorizationScopes[0] = authorizationScope;
return Collections.singletonList(
new SecurityReference("X-Token", authorizationScopes));
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("管理学院校友信息管理系统接口文档")
.version("1.0")
.contact(new Contact("zx", "https://blog.csdn.net/sober_pluto?type=blog","xxxx"))
.build();
}
}
<!--Swagger文档工具-->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
二、swagger注解
Swagger-Ul可以动态地根据注解生成在线API文档。常用注解
@Api:用于修饰Controller类,生成Controller相关文档信息。
@ApiOperation:用于修饰Controller类中的方法,生成接口方法相关文档信息。
@ApiParam:用于修饰接口中的参数,生成接口参数相关文档信息。
@ApiModelProperty:用于修饰实体类的属性,当实体类是请求参数或返回结果时,直接生成相关文档信息。