Spring 启动 + Springbox swagger 错误

Spring Boot + Springbox swagger error

我有一个 spring 引导项目想通过 springbox 与 swagger 集成。

我已经启动了 spring 启动应用程序并且 运行 一切正常。

但是我添加springbox后,单元测试无法通过

这是我在项目中添加的详细信息。

对于pom.xml,已添加

  <!--Swagger io for API doc-->
  <dependency>
        <groupId>io.swagger</groupId>
        <artifactId>swagger-core</artifactId>
        <version>1.5.3</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.2.2</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.2.2</version>
    </dependency>

然后使用 swagger 配置 class

@Configuration
@EnableSwagger2
public class SwaggerConfig {

@Bean
public Docket booksApi() {
    return new Docket(DocumentationType.SWAGGER_2)
            .apiInfo(apiInfo())
            .select()
            .apis(RequestHandlerSelectors.any())
            .paths(PathSelectors.regex("/.*"))
            .build();
}

private ApiInfo apiInfo() {
    return new ApiInfoBuilder()
            .title("blah")
            .description("blah.")
            .termsOfServiceUrl("http://www.blah.com.au")
            .contact("blah")
            .build();
}

}

我在 运行 mvn clean package 时遇到的错误是

  org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'webMvcRequestHandlerProvider' defined in URL [jar:file:/Users/jasonfeng/.m2/repository/io/springfox/springfox-spring-web/2.2.2/springfox-spring-web-2.2.2.jar!/springfox/documentation/spring/web/plugins/WebMvcRequestHandlerProvider.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [java.util.List]: : No qualifying bean of type [org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping] found for dependency [collection of org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping] found for dependency [collection of org.springframework.web.servlet.mvc.method.RequestMappingInfoHandlerMapping]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}

我使用的版本是

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.2.5.RELEASE</version>
</parent>

早上一直在研究这个问题,但运气不佳,然后发布了这个问题。刚刚发布问题,我找到了解决方案.....(我怪早上咖啡不太好)

只需去掉swagger配置中的@Configuration注解class。

这里是link我指的是

https://github.com/springfox/springfox/issues/462

我遇到了完全相同的问题。这是解决方案。

将此添加到应用程序-test.properties(如果尚不存在,请创建一个)

spring.profiles.active=test

注释测试(如果尚未存在)

@TestPropertySource(locations = "classpath:application-test.properties")

创建一个新的 Swagger 配置 class 并注释如下:

@Configuration
@EnableSwagger2
@Profile("!test")
public class SwaggerConfig {
    @Bean
    public Docket api() {
        .........
    }
}

这将确保根本不会加载 swagger 配置进行测试。

如下添加配置文件注释

@Profile("dev")
@Configuration
@EnableSwagger2
public class SwaggerConfig {

因此不会加载 swagger class 在 compile/build/test 生命周期中不会调用 并将以下 属性 添加到 application-test.properties(如果 src/test/resources 文件夹下不存在,请创建一个) spring.profiles.active=测试 为我解决了这个问题。