找不到swagger-resources / configuration / ui的映射

时间:2016-08-03 12:46:24

标签: java spring documentation swagger springfox

我正在尝试在非春季启动应用程序中配置swagger ui。我做了以下事情。 1.添加了以下依赖项

<dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.1.2</version>
    </dependency>

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

    <dependency>
        <groupId>org.webjars</groupId>
        <artifactId>bootstrap</artifactId>
        <version>3.3.5</version>
    </dependency>

2。添加了Swagger配置类

@Configuration
@EnableSwagger2
@EnableWebMvc
//@PropertySource("classpath:/swagger.properties")
public class SwaggerConfig {

@Bean
public Docket proposalApis(){
    return new Docket(DocumentationType.SWAGGER_2)
        .groupName("test")
        .select()
            .apis(RequestHandlerSelectors.basePackage("com.test.abc"))
        .paths(PathSelectors.regex("/test1.*"))
        .build()
        .apiInfo(testApiInfo());
}

private ApiInfo testApiInfo() {
    ApiInfo apiInfo = new ApiInfoBuilder().title("Test APIs").description("GET POST PUT methods are supported ").version("V1").build();
    return apiInfo;
}
}
  1. 添加了以下映射:

    <mvc:resources mapping="swagger-ui.html" location="classpath:/META-    INF/resources/"/>
    <mvc:resources mapping="/webjars/**" location="classpath:/META-    INF/resources/webjars/"/>
    
  2. 我可以访问以下网址

        /v2/api-docs
        /swagger-resources
    

    但是加载swagger-ui.html UI时会加载并且服务器上出现跟随错误 enter image description here

        No mapping found for /context/swagger-resources/configuration/ui in Dispatcher servlet
    

    有人可以帮忙吗?

2 个答案:

答案 0 :(得分:2)

我在我的pom中使用Swagger版本2.3.1。我想知道为什么你有springfox-swagger2springfox-swagger-ui工件的不同版本?

我的SwaggerConfig类看起来像这样。没有属性:

@EnableSwagger2
@Configuration
public class SwaggerConfig {
    @Autowired
    private TypeResolver typeResolver;

    @Bean
    public Docket swaggerSpringMvcPlugin() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("FooBar")
                .select()
                //Ignores controllers annotated with @CustomIgnore
                .apis(any()) //Selection by RequestHandler
                        .paths(paths()) // and by paths
                        .build()
                        .apiInfo(apiInfo()
                        );
    }

    private ApiInfo apiInfo() {
        return new ApiInfo("FooBar",
                "A java server based on SpringBoot",
                "1.0.0",
                null,
                "author","","");
    }

    //Here is an example where we select any api that matches one of these paths
    private Predicate<String> paths() {
        return or(
                regex("/foobar/*.*")
                );
    }
}

我没有配置或资源。

当我点击网址http://localhost:8080/foobar/swagger-ui.html

时,页面会出现

答案 1 :(得分:0)

springfox-swagger2和springfox-swagger-ui的不同版本一直是个问题。在某些情况下,如2.5.0之前的版本和2.6.1版本的后者,集成工作正常。但是,如果前者是2.6.1而后者是2.4.0,则ui变得不相容。因此,我建议如果两个依赖关系都是通过实践采用相同的版本,那么可以减少swagger的意外功能。