Spring Boot + Springbox招摇错误

jas*_*ing 14 java spring swagger spring-boot

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

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

但是在我添加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>
Run Code Online (Sandbox Code Playgroud)

然后使用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();
}

}
Run Code Online (Sandbox Code Playgroud)

运行时我得到的错误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: {}
Run Code Online (Sandbox Code Playgroud)

我正在使用的版本是

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.2.5.RELEASE</version>
</parent>
Run Code Online (Sandbox Code Playgroud)

jas*_*ing 22

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

只需删除@Configurationswagger配置类中的注释即可.

这是我所指的链接

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

  • 当我删除配置注释时,我弹出窗口:“无法推断基本URL。在使用动态Servlet注册或API位于API网关后面时,这很常见。基本URL是所有招摇的地方的根。资源”。下面的配置文件解决方案有效。 (2认同)

小智 12

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

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

spring.profiles.active=test
Run Code Online (Sandbox Code Playgroud)

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

@TestPropertySource(locations = "classpath:application-test.properties")
Run Code Online (Sandbox Code Playgroud)

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

@Configuration
@EnableSwagger2
@Profile("!test")
public class SwaggerConfig {
    @Bean
    public Docket api() {
        .........
    }
}
Run Code Online (Sandbox Code Playgroud)

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