Swagger 不会选择自定义的 API 信息并始终显示默认值 [英] Swagger doesn't pick up customized API Info and always shows default values
问题描述
当我尝试将 Swagger 集成到一个非常简单的 Spring Boot REST 应用程序时,Swagger-UI.html 不会显示和获取我自定义的 API 信息.我应该如何更改以下代码,以便 Swagger UI 页面显示自定义的 API 信息?我也无法调试 SwaggerConfig 类,将断点放入但是当作为 Spring Boot 应用程序运行时,断点不会停止.
While I am trying to integrate Swagger into a very simple Spring Boot REST app, Swagger-UI.html won't display and pick up my customized API Info. How should I make changes to below code so Swagger UI page will display the customized API Information? I cannot debug the SwaggerConfig class as well, put breakpoints in but when run as Spring Boot app, breakpoints won't stop.
我在 pom.xml 中有什么:
What I have in pom.xml:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>
我的 SwaggerConfig 类:
My SwaggerConfig Class:
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket messageApi() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("cool-report-api")
.apiInfo(apiInfo())
.select()
.paths(messageApiPaths()).build();
}
private Predicate<String> messageApiPaths() {
return or(regex("/api/topics.*"), regex("/api/message.*"));
}
private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Cool Message Receiver API")
.description("Cool Message Receiver REST API Reference")
.termsOfServiceUrl("http://www.cool-message-receiver.com")
.contact(new Contact("John Smith", null, "john.smith@cool.com"))
.license("Cool Proprietary Software")
.licenseUrl("www.cool-message.com")
.version("0.1.0")
.build();
}
}
但是在我 spring-boot:run 上面的代码之后,自定义的 API 信息似乎不起作用,Swagger 仍然显示默认的Api 文档"标题和Apache 2.0"许可证等.这是我所看到的现在:
But after I spring-boot:run the above code, the customized API Info doesn't seem to work and Swagger still displays the default "Api Documentation" title and "Apache 2.0" license etc. Here is what I am seeing now:
推荐答案
apiInfo 应该像这样在构建后调用:
The apiInfo should be call after the build like this:
@Bean
public Docket messageApi() {
return new Docket(DocumentationType.SWAGGER_2)
.groupName("cool-report-api")
.select()
.paths(messageApiPaths())
.build()
.apiInfo(apiInfo());
}
这篇关于Swagger 不会选择自定义的 API 信息并始终显示默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!