请提出解决方案。我被困在这里。
@Configuration
@EnableSwagger2
public class SwaggerConfig {
@Bean
public Docket api() {
return new Docket(DocumentationType.SWAGGER_2);
}
}
以下是错误消息;
***************************申请无法开始
说明:
方法linkDiscoverers中的参数0 org.springframework.hateoas.config.HateoasConfiguration需要一个 单个豆,但发现了17个:
操作:
考虑将其中一个bean标记为@Primary,以更新使用者 接受多个bean,或使用@Qualifier标识bean 应该食用的
答案 0 :(得分:2)
您可以通过添加以下bean来更改配置类
@Bean
public LinkDiscoverers discoverers() {
List<LinkDiscoverer> plugins = new ArrayList<>();
plugins.add(new CollectionJsonLinkDiscoverer());
return new LinkDiscoverers(SimplePluginRegistry.create(plugins));
}
配置类如下:
@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));
}
}
答案 1 :(得分:0)
请您尝试以下配置类以解决此问题。
@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();
}
}
答案 2 :(得分:0)
我猜您正在使用Swagger 2.9.2和SpringBoot2.2.x。它具有open issue,具有兼容性。
答案 3 :(得分:0)
希望这会有所帮助
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;
}
}
具有这两个maven依赖项
<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>
答案 4 :(得分:0)
请确保您已将以下依赖项添加到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>
如果它不起作用,请提供更多信息。
答案 5 :(得分:0)
我制作的 BootApplication 如下
@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);
}
}
然后否定测试的配置文件以避免在测试过程中失败并设置swagger配置如下。
@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();
}
}