Swagger 2.0:swagger-ui 页面显示默认 api 信息而不是我正在设置的自定义 api 信息

Swagger 2.0: swagger-ui page showing default api info instead of the custom api info which I am setting

我正在使用 docket api 在 spring mvc 项目中配置 swagger。 swagger-ui 正确显示了所有路径。但是,标题、描述、许可证等自定义 API 信息仅显示默认值,而不是我设置的值。

调试中: 在 class "SwaggerConfiguration" 的方法 "apiDocket()" 中,我在 object "docket" 之前检查了字段 "apiInfo" 的值45=] 语句(使用反射)。这些值已正确设置。很明显,问题出在我无法检测到的其他地方。 我正在使用 sprinfox-swagger 版本 2.9.2。我试过 2.9.1、2.8.0、2.7.0 和 2.6.1 只是为了检查一下。我得到了相同的结果。 我面临的问题已在 Swagger doesn't pick up customized API Info and always shows default values and Spring boot swagger :custom information about Api not working 提出,但我还没有找到解决方案。

@Configuration
@EnableSwagger2
public class SwaggerConfiguration {

    @Bean
    public Docket apiDocket() {

        ApiInfo apiInfo = getApiInfo();
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.any())
                .paths(PathSelectors.any())
                .build();
        docket.apiInfo(apiInfo);
        return docket;
    }

    private ApiInfo getApiInfo() {

        return new ApiInfo(
                "TITLE",
                "DESCIPRION",
                "VERSION",
                "TERMS OF SERVICE URL",
                new Contact("NAME","URL","EMAIL"),
                "LICENSE",
                "LICENSE URL",
                Collections.emptyList()
        );
    }
}

在 servlet 配置 xml 文件中,我有以下内容:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd                 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd                 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">

    <!-- The controllers are autodetected POJOs labeled with the @Controller 
        annotation. -->
    <context:component-scan base-package="com.sample.package"
        use-default-filters="false">
        <context:include-filter
            expression="org.springframework.stereotype.Controller" type="annotation" />
        <context:include-filter type="annotation"
            expression="org.springframework.web.bind.annotation.ControllerAdvice" />
        <!-- <context:include-filter type="regex" expression=".*SwaggerConfiguration.*"/> -->
    </context:component-scan>

    <context:property-placeholder location="classpath*:META-INF/spring/*.properties"/>

    <!-- Turns on support for mapping requests to Spring MVC @Controller 
        methods Also registers default Formatters and Validators for use across all 
        @Controllers -->
    <mvc:annotation-driven />

    <mvc:default-servlet-handler />

    <mvc:interceptors>
    <bean class="com.sample.package.CustomRequestHandler"/>
    </mvc:interceptors>

    <!-- Enable scanning of spring @Configuration classes -->
    <context:annotation-config />

    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    </bean>
    <bean id="swagger2Config"
        class="springfox.documentation.swagger2.configuration.Swagger2DocumentationConfiguration">
    </bean>
    <mvc:resources order="1" location="/resources/"
    mapping="/resources/**" />
    <mvc:resources mapping="swagger-ui.html"
    location="classpath:/META-INF/resources/" />
    <mvc:resources mapping="/webjars/**"
    location="classpath:/META-INF/resources/webjars/" />
</beans>

在我的 pom.xml 我有

            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger2</artifactId>
                <version>2.9.2</version>
            </dependency>
            <dependency>
                <groupId>io.springfox</groupId>
                <artifactId>springfox-swagger-ui</artifactId>
                <version>2.9.2</version>
            </dependency>

在 swagger-ui 页面中,我希望标题为 "TITLE",描述为 "DESCRIPTION, version to be "VERSION",从代码中的方法 "getApiInfo()" 可以看出片段。但是,我得到的标题为 "Api Documentation",描述为 "Api Documentation",版本为“1.0”。这些都是默认值,我在代码中设置的值没有反映在swagger-ui 页面。 请帮忙!

在 servlet 配置 xml 文件中,我需要将 SwaggerConfiguration class 添加为 bean。我添加了以下行:

<bean class="com.sample.package.SwaggerConfiguration"/>

成功了。