【发布时间】:2020-07-03 12:39:28
【问题描述】:
我正在尝试将 swagger 集成到我的 Spring Boot 应用程序中。
春季版:1.2.2.RELEASE Java版本:8
Swagger Maven 依赖
<swagger.version>2.0.1</swagger.version>
<swagger-annotations.version>1.5.21</swagger-annotations.version>
<swagger-models.version>1.5.21</swagger-models.version>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger.version}</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger.version}</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
<version>${swagger-annotations.version}</version>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-models</artifactId>
<version>${swagger-models.version}</version>
</dependency>
配置文件是
@Configuration
@EnableSwagger2
@EnableAutoConfiguration
public class Swagger2UiConfiguration {
public Docket postsApi() {
return new Docket(DocumentationType.SWAGGER_2).groupName("public-api").select()
.apis(RequestHandlerSelectors.withClassAnnotation(RestController.class)).paths(PathSelectors.any())
.build();
}
@Bean
public Docket apiDoc(ServletContext servletContext) {
return new Docket(DocumentationType.SWAGGER_2)
.host("http://localhost:8080")
.pathProvider(new RelativePathProvider(servletContext) {
@Override
public String getApplicationBasePath() {
return "/services";
}
});
}
private Predicate<String> postPaths() {
return or(regex("/myproject/services/*"));
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder().title("EMP").description("EMP Swagger Test").version("1.0").build();
}
}
运行应用程序时,我可以看到以下日志,
2020-07-03 20:50:29,218 [ INFO] [] [localhost-startStop-1] (AbstractHandlerMethodMapping.java:220) - Mapped "{[/v2/api-docs],methods=[GET],params=[],headers=[],consumes=[],produces=[application/json || application/hal+json],custom=[]}" onto public org.springframework.http.ResponseEntity<springfox.documentation.spring.web.json.Json> springfox.documentation.swagger2.web.Swagger2Controller.getDocumentation(java.lang.String,javax.servlet.http.HttpServletRequest)
2020-07-03 20:50:29,225 [ INFO] [] [localhost-startStop-1] (AbstractHandlerMethodMapping.java:220) - Mapped "{[/swagger-resources/configuration/security],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto org.springframework.http.ResponseEntity<springfox.documentation.swagger.web.SecurityConfiguration> springfox.documentation.swagger.web.ApiResourceController.securityConfiguration()
2020-07-03 20:50:29,227 [ INFO] [] [localhost-startStop-1] (AbstractHandlerMethodMapping.java:220) - Mapped "{[/swagger-resources/configuration/ui],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto org.springframework.http.ResponseEntity<springfox.documentation.swagger.web.UiConfiguration> springfox.documentation.swagger.web.ApiResourceController.uiConfiguration()
2020-07-03 20:50:29,228 [ INFO] [] [localhost-startStop-1] (AbstractHandlerMethodMapping.java:220) - Mapped "{[/swagger-resources],methods=[],params=[],headers=[],consumes=[],produces=[],custom=[]}" onto org.springframework.http.ResponseEntity<java.util.List<springfox.documentation.swagger.web.SwaggerResource>> springfox.documentation.swagger.web.ApiResourceController.swaggerResources()
Swagger-UI URL 仍然返回 404。
http://localhost:8080/swagger-ui.html
http://localhost:8080/myproject/services/swagger-ui.html
我错过了什么吗?任何帮助都非常有用。
P.S:在谷歌上搜索了这么多页面后,我怀疑它可能是版本兼容性。如果是这样,我无法升级我的 spring-boot 版本。在那种情况下,我该如何集成 Swagger?有什么建议吗?
【问题讨论】:
标签: java spring-boot swagger swagger-2.0 springfox