I have a spring boot project want to integrate with swagger via springbox.
I have my spring boot app up and running all good.
However after I added springbox, it can not pass unit test.
Here are the details I added in project.
For pom.xml
, added
<!--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
@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
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>
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.
Here is the link I refer to
I was facing the exact same issue. Here is the solution.
Add this to application-test.properties (Create one if not already present)
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:
@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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.