Swagger
swagger的使用如下
1.添加swagger2 依赖
<!-- swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
<exclusions>
<exclusion>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
</exclusion>
<exclusion>
<groupId>io.swagger</groupId>
<artifactId>swagger-models</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>1.5.24</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-models</artifactId>
<version>1.5.24</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
2.配置swagger
package com.xtc.afterservice;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.SecurityReference;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger.web.OperationsSorter;
import springfox.documentation.swagger.web.UiConfiguration;
import springfox.documentation.swagger.web.UiConfigurationBuilder;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.ArrayList;
import java.util.List;
@Profile("dev")// 选择配置文件
@Configuration
@EnableSwagger2 // 开启swagger2
public class AfterServiceSwagger {
@Bean
public Docket createTestRestApi() {
String groupName = "网点域";
String basePackage = "com.xtc.servicecenter.web.site";// 目录下是一些Controller
return createRestApi(groupName, basePackage);
}
@Bean
public UiConfiguration createUiConfiguration() {
return UiConfigurationBuilder.builder()
.operationsSorter(OperationsSorter.METHOD)
.build();
}
private Docket createRestApi(String groupName, String basePackage) {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo(groupName))// 配置swagger页面信息(dto)
.enable(true)// 默认true开启swagger,false关闭
.groupName(groupName)// 分组,不同域下是不同的Controller
.securitySchemes(securitySchemes())// 设置安全方案
.securityContexts(securityContexts())// 设置安全上下文
.select()// 返回对象
.apis(RequestHandlerSelectors.basePackage(basePackage))// 指定扫描包
.paths(PathSelectors.any())// 指定不扫描包
.build();
}
private ApiInfo apiInfo(String groupName) {
return new ApiInfoBuilder()
.title("售后服务平台API")
.build();
}
private List<ApiKey> securitySchemes() {
List<ApiKey> apiKeys = new ArrayList<>();
apiKeys.add(new ApiKey("Authorization", "Authorization", "header"));
return apiKeys;
}
private List<SecurityContext> securityContexts() {
List<SecurityContext> securityContexts = new ArrayList<>();
securityContexts.add(SecurityContext.builder()
.securityReferences(defaultAuth())
.forPaths(PathSelectors.regex("^(?!auth).*$")).build());
return securityContexts;
}
private List<SecurityReference> defaultAuth() {
AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
authorizationScopes[0] = authorizationScope;
List<SecurityReference> securityReferences = new ArrayList<>();
securityReferences.add(new SecurityReference("Authorization", authorizationScopes));
return securityReferences;
}
}
有些需要二级token,需要在idea的项目配置域,然后再swagger输入域的token
使用swagger注解
pojo实体类层的类上用@ApiModel,属性上用@ApiModelProperty
controller控制层的类上用@Api注解,方法上用@ApiOperation,方法参数上用@ApiParam
属性:
name–参数名
value–参数说明
required–是否必填
@ApiIgnore(),方法或者类被忽略,可以不被swagger显示在页面上,基本不用
@ApiImplicitParams() 用于controller层的方法上,包含多个 @ApiImplicitParam
@ApiImplicitParam() 用于controller层的方法上,表示单独的请求参数
登录swagger UI界面
url必须是本地IP+端口,比如http://localhost:8080/swagger-ui.html
右上角 Select a spec 选择default,表示显示本地全部接口,如果有域,可以选择域的来使用
右侧 Authorize 是填token,在测试环境的app页面查看,找到Authorization后面的,Bearer eyJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJiYmsiLCJleHAiOjE2NTMyODE4MTcsInVzZXJuYW1lIjoiYWRtaW4ifQ.2SpU-xEjRn3XPlofxdKmfG4paFtCtsADuUD0CfcGfb4就是token,复制粘贴过来即可