web-dev-qa-db-fra.com

Spring Boot + Springbox swagger erreur

J'ai un projet de bottes de printemps que je souhaite intégrer à swagger via springbox.

J'ai mon application de démarrage de printemps en marche et tout fonctionne bien.

Cependant, après avoir ajouté springbox, il ne peut pas réussir le test unitaire.

Voici les détails que j'ai ajoutés dans le projet.

Pour pom.xml, ajouté

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

puis avec une classe de configuration 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();
}

}

L'erreur que je reçois quand run mvn clean package est

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

la version que j'utilise est

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.2.5.RELEASE</version>
</parent>
12
jasonfungsing

Je me suis penché sur ce problème pendant la matinée sans succès, puis j'ai posté cette question. Juste après la publication de la question, j'ai trouvé la solution à ce problème ..... (je blâme le café du matin pas terrible)

Supprimez simplement l'annotation @Configuration dans la classe de configuration swagger.

Voici le lien auquel je me réfère 

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

18
jasonfungsing

Je faisais face au même problème. Voici la solution.

Ajouter ceci à application-test.properties (en créer un s'il n'est pas déjà présent)

spring.profiles.active=test

Annoter le test (s'il n'est pas déjà présent)

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

Créez une nouvelle classe de configuration Swagger et annotez-la comme suit:

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

Cela garantira que swagger config n’est pas chargé pour le test.

10
Nish

Ajouter une annotation de profil comme ci-dessous

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

pour que swagger ne soit pas chargé, cette classe n'est pas appelée pendant le cycle de vie de compilation/test/ et ajoutez la propriété ci-dessous à application-test.properties (créez-en un s'il n'est pas déjà présent dans le dossier src/test/resources spring.profiles.active = test a résolu le problème pour moi.

0
user1419261