java Spring Boot + Springbox 招摇错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32449612/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring Boot + Springbox swagger error
提问by jasonfungsing
I have a spring boot project want to integrate with swagger via springbox.
我有一个 spring boot 项目想通过 springbox 与 swagger 集成。
I have my spring boot app up and running all good.
我的 Spring Boot 应用程序启动并运行良好。
However after I added springbox, it can not pass unit test.
但是在我添加 springbox 后,它无法通过单元测试。
Here are the details I added in project.
这是我在项目中添加的详细信息。
For pom.xml
, added
对于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>
then with a swagger config class
然后使用 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();
}
}
The error I am getting when run mvn clean package
is
我在运行时遇到的错误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: {}
the version I am using is
我使用的版本是
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.5.RELEASE</version>
</parent>
回答by jasonfungsing
Been looking into this problem for the while morning without luck, then posted this question. Just after posted the question, I found out the solution for this..... (I blame on the not-so-good morning coffee)
在没有运气的情况下早上一直在研究这个问题,然后发布了这个问题。在发布问题后,我找到了解决方案.....(我归咎于不太好的早晨咖啡)
Simply remove the @Configuration
annotation in the swagger configuration class.
只需删除@Configuration
swagger 配置类中的注释。
Here is the link I refer to
这是我参考的链接
回答by Nish
I was facing the exact same issue. Here is the solution.
我面临着完全相同的问题。这是解决方案。
Add this to application-test.properties (Create one if not already present)
将此添加到 application-test.properties (如果尚不存在,请创建一个)
spring.profiles.active=test
Annotate the test (if not already present)
注释测试(如果尚未存在)
@TestPropertySource(locations = "classpath:application-test.properties")
Create a new Swagger Configuration class and annotate it as following:
创建一个新的 Swagger 配置类并注释如下:
@Configuration
@EnableSwagger2
@Profile("!test")
public class SwaggerConfig {
@Bean
public Docket api() {
.........
}
}
This will make sure that swagger config is not loaded for test at all.
这将确保根本不会加载 swagger 配置进行测试。
回答by user1419261
Add a Profile annotation as below
添加配置文件注释如下
@Profile("dev")
@Configuration
@EnableSwagger2
public class SwaggerConfig {
so that swagger is not loaded this class not invoked during the compile/build/test life cycle and Add the below property to application-test.properties (Create one if not already present under src/test/resources folder) spring.profiles.active=test resolved the issue for me.
这样就不会加载 swagger 在编译/构建/测试生命周期中未调用的此类,并将以下属性添加到 application-test.properties(如果 src/test/resources 文件夹下尚未存在,则创建一个) spring.profiles.active =test 为我解决了这个问题。