Spring Boot + Springbox招摇错误

我有一个春季启动项目希望通过springbox与swagger集成。

我的弹簧启动应用程序运行良好。

但是在我添加springbox之后,它无法通过unit testing。

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

对于pom.xml ,添加了

    io.swagger swagger-core 1.5.3   io.springfox springfox-swagger2 2.2.2   io.springfox springfox-swagger-ui 2.2.2  

然后使用swagger配置类

 @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: {} 

我正在使用的版本是

  org.springframework.boot spring-boot-starter-parent 1.2.5.RELEASE  

在没有运气的情况下一直在调查这个问题,然后发布了这个问题。 在发布问题之后,我发现了这个问题的解决方案…..(我责怪不那么好的早晨咖啡)

只需删除swagger配置类中的@Configuration注释即可。

这是我所指的链接

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

我面临着同样的问题。 这是解决方案。

将其添加到application-test.properties(如果尚未创建,则创建一个)

 spring.profiles.active=test 

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

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

创建一个新的Swagger Configuration类并对其进行注释,如下所示:

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

这将确保未加载swagger配置进行测试。

添加配置文件注释,如下所示

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

所以没有加载swagger这个类在编译/构建/测试生命周期中没有被调用,并且将下面的属性添加到application-test.properties(如果src / test / resources文件夹下没有,则创建一个)spring.profiles.active =测试为我解决了这个问题。