我的系统采用的是Spring boot.
总之部署测试环境后遇到了个问题, 需要把Swagger UI 发出的请求的URL 添加 .do后缀。
Spring 配置:
#开发环境
spring:
profiles: dev
log:
path: /Volumes/data/logs/box-web
level: INFO
server:
port: 8086
contextPath: /box/v1
Swaager Config:
主要就是自定义 CustRelativePathProvider 类,把url + 一个 .do 后缀。
@Configuration
public class Swagger2Config {
@Value("${server.contextPath}")
private String contextPath;
@Bean
public Docket createRestApi() {
Docket docket = new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).host("abc.com:80")
.useDefaultResponseMessages(false).pathProvider(new CustRelativePathProvider()).select()
.apis(RequestHandlerSelectors.basePackage("com.abc.box")).paths(PathSelectors.any()).build();
return docket;
}
public class CustRelativePathProvider extends AbstractPathProvider {
public static final String ROOT = "/";
@Override
public String getOperationPath(String operationPath) {
UriComponentsBuilder uriComponentsBuilder = UriComponentsBuilder.fromPath("/");
String uri = removeAdjacentForwardSlashes(uriComponentsBuilder.path(operationPath).build().toString());
return uri + ".do";
}
@Override
protected String applicationPath() {
return isNullOrEmpty(contextPath) ? ROOT : contextPath;
}
@Override
protected String getDocumentationPath() {
return ROOT;
}
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder().title("后端API文档")
.description("API根地址:http://com.abc.com/box/v1/swagger-ui.html")
.termsOfServiceUrl("http://com.abc.com/box/v1/swagger-ui.html")
.contact(new Contact("**数据", "abc.com", "chengongmo@126.com")).version("v1").build();
}
}