【问题标题】:Swagger doesn't pick up customized API Info and always shows default valuesSwagger 不会获取自定义的 API 信息并始终显示默认值
【发布时间】:2018-05-24 08:59:11
【问题描述】:

当我尝试将 Swagger 集成到一个非常简单的 Spring Boot REST 应用程序中时,Swagger-UI.html 不会显示和获取我自定义的 API 信息。我应该如何更改以下代码,以便 Swagger UI 页面显示自定义的 API 信息?我也无法调试 SwaggerConfig 类,将断点放入,但是当作为 Spring Boot 应用程序运行时,断点不会停止。

我在 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 类:

@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 Info 似乎不起作用,Swagger 仍然显示默认的“Api Documentation”标题和“Apache 2.0”许可证等。这是我看到的现在:

【问题讨论】:

    标签: spring maven spring-boot swagger-ui swagger-2.0


    【解决方案1】:

    我认为问题在于 messageApisPath()。

    确保添加以下库:

        <dependency>
            <groupId>com.google.guava</groupId>
            <artifactId>guava</artifactId>
            <version>16.0.1</version>
        </dependency>
    

    它包含很多谓词函数。确保你的“或”来自那里。

    以下对我有用

    private Predicate<String> messageApiPaths() {
        return Predicates.or(PathSelectors.regex("/api/topics.*"), PathSelectors.regex("/api/message.*"));
    }
    

    谢谢

    【讨论】:

    • 感谢您的意见,已尝试但没有解决问题。症状是没有获取 API 信息。 API 路径很好。
    • 我很遗憾听到这个消息。 :(
    【解决方案2】:

    应该像这样在构建之后调用 apiInfo:

    @Bean
    public Docket messageApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("cool-report-api")
                .select()
                .paths(messageApiPaths())
                .build()
                .apiInfo(apiInfo());
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-06-16
      • 1970-01-01
      • 1970-01-01
      • 2017-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多