Spring Boot + Springbox 招摇错误

2022-01-22 00:00:00 swagger spring java spring-boot

我有一个spring boot项目想通过springbox与swagger集成.

I have a spring boot project want to integrate with swagger via springbox.

我的 Spring Boot 应用程序启动并运行良好.

I have my spring boot app up and running all good.

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

However after I added springbox, it can not pass unit test.

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

Here are the details I added in project.

对于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

@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 时遇到的错误是

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

我使用的版本是

<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)

只需去掉swagger配置类中的@Configuration注解即可.

Simply remove the @Configuration annotation in the swagger configuration class.

这是我参考的链接

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

相关文章