Veuillez suggérer comment résoudre ce problème. Je suis bloqué ici.
@Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2); } }
Le message d'erreur suit;
*************************** DÉMARRAGE ÉCHEC DE L'APPLICATION
Description:
Paramètre 0 de la méthode linkDiscoverers dans org.springframework.hateoas.config.HateoasConfiguration nécessitait un haricot unique, mais 17 ont été trouvés:
Action:
Pensez à marquer l'un des beans comme @Primary, en mettant à jour le consommateur pour accepter plusieurs beans, ou en utilisant @Qualifier pour identifier le bean qui devrait être consommé
6 Réponses :
Pouvez-vous essayer ci-dessous la classe de configuration pour résoudre ce problème.
@Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2).select() .apis(RequestHandlerSelectors.basePackage("package Name")).paths(PathSelectors.any()) .build(); } }
Je suppose que vous utilisez Swagger 2.9.2 et SpringBoot 2.2.x. Il a un problème ouvert avec compatibilité.
oui, j'utilise la version 2.9.2 et je suppose que mon springboot est 2.2 ou supérieur.
J'espère que cela aidera
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency>
Avec ces deux dépendances maven
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket siteOfficeApi() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors .basePackage("Basepackage")) .paths(PathSelectors.any()) .build() .apiInfo(metaData()); } private ApiInfo metaData() { ApiInfo apiInfo = new ApiInfo( "Title", "Description", "Version", "Terms of service", "Contact Name", "License", "Licence URL"); return apiInfo; } }
Assurez-vous d'avoir ajouté les dépendances suivantes à votre pom.
<dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>**[version here]**</version> <exclusions> <exclusion> <groupId>org.mapstruct</groupId> <artifactId>mapstruct-jdk8</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>**[version here]**</version> </dependency>
Si cela ne fonctionne pas, veuillez fournir plus d'informations.
Cela n'a pas fonctionné. J'utilise spring boot 2.2.0 et swagger versio 2.9.2 sur les deux dépendances swagger.
J'utilise des versions plus anciennes et elles fonctionnent bien pour moi ... comme d'autres l'ont dit, il pourrait y avoir des problèmes de compatibilité ... vous pouvez donc passer à une autre version ou attendre ...
Vous pouvez modifier votre classe de configuration en ajoutant le bean suivant
@Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2); } @Bean public LinkDiscoverers discoverers() { List<LinkDiscoverer> plugins = new ArrayList<>(); plugins.add(new CollectionJsonLinkDiscoverer()); return new LinkDiscoverers(SimplePluginRegistry.create(plugins)); } }
la classe de configuration ressemble à ceci:
@Bean public LinkDiscoverers discoverers() { List<LinkDiscoverer> plugins = new ArrayList<>(); plugins.add(new CollectionJsonLinkDiscoverer()); return new LinkDiscoverers(SimplePluginRegistry.create(plugins)); }
impossible de résoudre «LinkDiscoverers» est affiché. comment le gérer?
J'ai fait le BootApplication comme ci-dessous
@Profile({"local","dev", "deva","devb","devc","staging","prod", "!test"}) @EnableSwagger2 @Configuration public class SwaggerConfig /*extends WebMvcConfigurationSupport*/ { public Docket productApi() { return new Docket(DocumentationType.SWAGGER_2).select() .apis(RequestHandlerSelectors.basePackage("com.wellsfargo.fcp.uae.goaml.controller")) .paths(PathSelectors.any()) .build(); } /* @Bean public LinkDiscoverers discoverers() { List<LinkDiscoverer> plugins = new ArrayList<>(); plugins.add(new CollectionJsonLinkDiscoverer()); return new LinkDiscoverers(SimplePluginRegistry.create(plugins)); }*/ private ApiInfo metaData() { /*ApiInfo apiInfo = new ApiInfoBuilder("Spring Boot REST API", "Spring Boot REST API for Online Store", "1.0", "Terms of service", new Contact("Hatice Sigirci", null, "hatice.sigirci@hotmail.com"), "Apache License Version 2.0", "https://www.apache.org/licenses/LICENSE-2.0", null); return apiInfo;*/ return new ApiInfoBuilder().title("Boot REST API") .description("Boot REST API reference ") .termsOfServiceUrl("http://tchelpcom") .contact("http://tchelp com").license("© All rights reserved") .licenseUrl("http://tchelp.com").version("1.0").build(); } }
}
puis annule le profil pour le test pour éviter l'échec pendant le test et définissez la configuration swagger comme ci-dessous. p >
@SpringBootApplication @EnableWebSecurity public class BootApplication extends WebSecurityConfigurerAdapter implements WebMvcConfigurer { @Override protected void configure(HttpSecurity http) throws Exception { http.csrf().disable(); } @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/swagger-ui.html**") .addResourceLocations("classpath:/META-INF/resources/swagger-ui.html"); registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/"); registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/"); } @Bean public LinkDiscoverers discoverers() { List<LinkDiscoverer> plugins = new ArrayList<>(); plugins.add(new CollectionJsonLinkDiscoverer()); return new LinkDiscoverers(SimplePluginRegistry.create(plugins)); } @Bean public BootApplicationProperties bootApplicationProperties() { return new BootApplicationProperties(); } /** * main for running the app. * * @param args main arguments */ public static void main(String[] args) { SpringApplication.run(BootApplication.class, args); }