swagger全局配置
一、統一增加URL前綴
@Configuration
@EnableSwagger2
@Profile({"test","dev"})
public class Swagger2Config implements WebMvcConfigurer {
@Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.apis(RequestHandlerSelectors.basePackage("com.xxx.controller"))
.paths(PathSelectors.any())
.build().pathMapping("/prefix/"); // 所有接口統一增加prefix前綴
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("XXX")
.description("XXXX")
.version("1.0")
.build();
}
}
二、統一增加參數
@Configuration
@EnableSwagger2
@Profile({"test","dev"})
public class Swagger2Config implements WebMvcConfigurer {
@Bean
public Docket createRestApi() {
List<Parameter> parameter = new ArrayList<Parameter>();
parameter.add(new ParameterBuilder().name("sign").description("簽名").required(true).modelRef(new ModelRef("string")).parameterType("query").build());
// 增加sign參數
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))
.apis(RequestHandlerSelectors.basePackage("com.xxxx.controller"))
.paths(PathSelectors.any())
.build().globalOperationParameters(parameter);
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("XXXX")
.description("XXXX")
.version("1.0")
.build();
}
}