我正在使用docket api在spring mvc项目中配置swagger。 swagger-ui正确显示了所有路径。但是,自定义API信息(例如标题,描述,许可证等)仅显示默认值,而不显示我正在设置的值。
调试: 在类“ SwaggerConfiguration”中的方法“ apiDocket()”中,我在返回语句之前(使用反射)检查了对象“ docket”的字段“ apiInfo”的值。正确设置了值。显然,问题出在我无法检测到的其他地方。 我正在使用2.9.2版的sprinfox-swagger。我尝试使用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和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 config 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文档”,描述为“ Api文档”,版本为“ 1.0”,这些都是默认值,我在代码中设置的值未反映在swagger-ui页面中。 请帮忙!
答案 0 :(得分:0)
在servlet config xml文件中,我需要将SwaggerConfiguration类添加为bean。我添加了以下行:
<bean class="com.sample.package.SwaggerConfiguration"/>
它奏效了。