有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

spring为什么我的大摇大摆不能用springboot在java中工作?

请建议如何解决此问题。我被困在这里了

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket api() {

        return new Docket(DocumentationType.SWAGGER_2);

    }
}

错误消息如下

  • ****************************应用程序无法启动


    说明:

    中方法链接发现者的参数0 组织。springframework。哈提奥斯。配置。HATEOASCO配置需要一个 单个bean,但找到17个:

    • modelBuilderPluginRegistry:在null中定义
    • modelPropertyBuilderPluginRegistry:在null中定义
    • typeNameProviderPluginRegistry:在null中定义
    • syntheticModelProviderPluginRegistry:在null中定义
    • documentationPluginRegistry:在null中定义
    • apiListingBuilderPluginRegistry:在null中定义
    • operationBuilderPluginRegistry:在null中定义
    • parameterBuilderPluginRegistry:在null中定义
    • expandedParameterBuilderPluginRegistry:在null中定义
    • resourceGroupingStrategyRegistry:在null中定义
    • operationModelsProviderPluginRegistry:在null中定义
    • defaultsProviderPluginRegistry:在null中定义
    • pathDecoratorRegistry:在null中定义
    • apiListingScannerPluginRegistry:在null中定义
    • relProviderPluginRegistry:由类路径资源中的方法“relProviderPluginRegistry”定义 [org/springframework/hateoas/config/HateoasConfiguration.class]
    • LinkDiscoveryrRegistry:在null中定义
    • entityLinksPluginRegistry:由类路径资源中的方法“entityLinksPluginRegistry”定义 [org/springframework/hateoas/config/webmvcentitylinkscoconfiguration.class]

    行动:

    考虑将bean中的一个标记为“初级”,更新消费者。 接受多个bean,或使用@Qualifier标识bean 应该被消耗掉的


共 (6) 个答案

  1. # 1 楼答案

    希望这会有所帮助

    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>
    
  2. # 2 楼答案

    我猜您使用的是Swagger 2.9.2和SpringBoot 2.2。x、 它有一个兼容的open issue

  3. # 3 楼答案

    请尝试下面的配置类来修复此问题

    @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();
    
        }
    }
    
  4. # 4 楼答案

    确保将以下依赖项添加到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. # 5 楼答案

    我做了如下的应用程序

    @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);
    }
    

    }

    然后取消测试配置文件,以避免在测试过程中失败,并按如下所示设置招摇过市配置

        @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();
            }
    
        }
    
    
              
    
  6. # 6 楼答案

    您可以通过添加以下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));
        }
    }