swagger 2文档无法正常工作

时间:2017-09-13 10:02:05

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

我试图使用swagger2和springfox来实现api文档。 我的项目不是弹簧启动或maven,它依赖于xml文件。

我添加了课程:

  

SwaggerConfig

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;



@EnableSwagger2
@PropertySource("classpath:swagger.properties")
@ComponentScan(basePackageClasses = ProductController.class)
@Configuration
public class SwaggerConfig {

    private static final String SWAGGER_API_VERSION = "1.0";
    private static final String LICENSE_TEXT = "License";
    private static final String title = "Products REST API";
    private static final String description = "RESTful API for Products";

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(title)
                .description(description)
                .license(LICENSE_TEXT)
                .version(SWAGGER_API_VERSION)
                .build();
    }

    @Bean
    public Docket productsApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .pathMapping("/")
                .select()
                .paths(PathSelectors.regex( "/*"))
                .build();
    }


}

所以当我运行tomcat服务器时,它运行正常而没有错误但是当我把以下链接:

  

http://localhost:8080/swagger-ui.html

没有任何反应。 我错过了一些配置或任何建议吗?

提前谢谢。

1 个答案:

答案 0 :(得分:1)

在我的

中添加这些语句后,

问题得以解决

  

弹簧-config.xml中

<mvc:default-servlet-handler/>
        <mvc:annotation-driven/>
        <mvc:resources location="classpath:/META-INF/resources/" mapping="swagger-ui.html"></mvc:resources>
        <mvc:resources location="classpath:/META-INF/resources/webjars/" mapping="/webjars/**"></mvc:resources>
        <bean class="springfox.documentation.swagger2.configuration.Swagger2DocumentationConfiguration" id="swagger2Config"/>