【问题标题】:Swagger 2.0: swagger-ui page showing default api info instead of the custom api info which I am settingSwagger 2.0:swagger-ui 页面显示默认 api 信息而不是我设置的自定义 api 信息
【发布时间】:2019-06-06 11:47:33
【问题描述】:

我正在使用 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 doesn't pick up customized API Info and always shows default valuesSpring 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”,从代码 sn-p 中的方法“getApiInfo()”可以看出。但是,我的标题为“Api Documentation”,描述为“Api Documentation”,版本为“1.0”。这些都是默认值,我在代码中设置的值没有反映在 swagger-ui页。 请帮忙!

【问题讨论】:

  • 虽然这并不能解决问题,但请转用 ApiInfoBuilder : private ApiInfo apiInfo() { return new ApiInfoBuilder().title(PNOSwaggerNotes.TITLE).description(PNOSwaggerNotes.DESCRIPTION) .contact(new Contact(PNOSwaggerNotes.CONTACT_NAME, PNOSwaggerNotes.CONTACT_URL, PNOSwaggerNotes.CONTACT_EMAIL)) .version("1.0").build(); }
  • @TechFree 感谢您的建议。我开始只使用 ApiInfoBuilder。我删除了该代码只是为了确保它不是我的问题的原因。一旦我解决了我的问题,我会回到它。
  • 还是不行?哪个 Spring 版本?
  • @TechFree Spring 版本 5.1.7.RELEASE(之前,我尝试使用版本 5.1.2.RELEASE)。
  • 任何解决方案,任何人?

标签: java spring-mvc swagger swagger-ui swagger-2.0


【解决方案1】:

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

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

它奏效了。

【讨论】:

    猜你喜欢
    • 2018-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-02
    • 1970-01-01
    • 2021-04-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多