首页 > 解决方案 > Swagger 2.0:swagger-ui 页面显示默认 api 信息而不是我设置的自定义 api 信息

问题描述

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

调试:在“SwaggerConfiguration”类的方法“apiDocket()”中,我在返回语句之前检查了对象“docket”的字段“apiInfo”的值(使用反射)。这些值已正确设置。很明显,问题出在我无法检测到的其他地方。我正在使用 sprinfox-swagger 版本 2.9.2。我尝试使用 2.9.1、2.8.0、2.7.0 和 2.6.1 进行检查。我得到了同样的结果。我在Swagger 上提出了我面临的问题,它没有获取自定义的 API 信息,并且总是显示默认值和Spring boot swagger:关于 Api 的自定义信息不起作用,但我没有得到我的解决方案。

@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”,从代码片段中的方法“getApiInfo()”可以看出。但是,我得到标题为“Api Documentation”,描述为“Api Documentation”,版本为“1.0”。这些都是默认值,我在代码中设置的值没有反映在 swagger-ui 页面中。请帮助!

标签: javaspring-mvcswaggerswagger-uiswagger-2.0

解决方案


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

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

它奏效了。


推荐阅读