Why HATEOAS starts creating issue for spring-boot version >= 2.2.x during startup with Swagger 2.x?

前端 未结 14 2187
忘掉有多难
忘掉有多难 2020-12-09 15:18

I moved my project from spring-boot 2.1.9 to 2.2.0.

While starting the project, I am facing the below error messages.

Caused by: org.sprin         


        
相关标签:
14条回答
  • 2020-12-09 16:02

    Resolved it, it was happening due to integration when Swagger + HATEOAS was used with Spring Boot 2.2.4.RELEASE

    package com.company.springbootworks.swagger;
    
    import java.time.LocalDate;
    import java.util.ArrayList;
    import java.util.List;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.hateoas.client.LinkDiscoverer;
    import org.springframework.hateoas.client.LinkDiscoverers;
    import org.springframework.hateoas.mediatype.collectionjson.CollectionJsonLinkDiscoverer;
    import org.springframework.http.ResponseEntity;
    import org.springframework.plugin.core.SimplePluginRegistry;
    
    import springfox.documentation.builders.ApiInfoBuilder;
    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.swagger.web.DocExpansion;
    import springfox.documentation.swagger.web.ModelRendering;
    import springfox.documentation.swagger.web.OperationsSorter;
    import springfox.documentation.swagger.web.TagsSorter;
    import springfox.documentation.swagger.web.UiConfiguration;
    import springfox.documentation.swagger.web.UiConfigurationBuilder;
    import springfox.documentation.swagger2.annotations.EnableSwagger2;
    
    @Configuration
    @EnableSwagger2
    public class SwaggerConfig {
    
        @Bean
        public LinkDiscoverers discoverers() {
            List<LinkDiscoverer> plugins = new ArrayList<>();
            plugins.add(new CollectionJsonLinkDiscoverer());
            return new LinkDiscoverers(SimplePluginRegistry.create(plugins));
    
        }
    
        @Bean
        public Docket eDesignApi(SwaggerConfigProperties swaggerConfigProperties) {
            return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo(swaggerConfigProperties))
                    .enable(Boolean.valueOf(swaggerConfigProperties.getEnabled())).select()
                    .apis(RequestHandlerSelectors.any()).paths(PathSelectors.any()).build().pathMapping("/")
                    .directModelSubstitute(LocalDate.class, String.class).genericModelSubstitutes(ResponseEntity.class)
                    .useDefaultResponseMessages(Boolean.valueOf(swaggerConfigProperties.getUseDefaultResponseMessages()))
                    .enableUrlTemplating(Boolean.valueOf(swaggerConfigProperties.getEnableUrlTemplating()));
        }
    
        @Bean
        UiConfiguration uiConfig(SwaggerConfigProperties swaggerConfigProperties) {
            return UiConfigurationBuilder.builder().deepLinking(Boolean.valueOf(swaggerConfigProperties.getDeepLinking()))
                    .displayOperationId(Boolean.valueOf(swaggerConfigProperties.getDisplayOperationId()))
                    .defaultModelsExpandDepth(Integer.valueOf(swaggerConfigProperties.getDefaultModelsExpandDepth()))
                    .defaultModelExpandDepth(Integer.valueOf(swaggerConfigProperties.getDefaultModelExpandDepth()))
                    .defaultModelRendering(ModelRendering.EXAMPLE)
                    .displayRequestDuration(Boolean.valueOf(swaggerConfigProperties.getDisplayRequestDuration()))
                    .docExpansion(DocExpansion.NONE).filter(Boolean.valueOf(swaggerConfigProperties.getFilter()))
                    .maxDisplayedTags(Integer.valueOf(swaggerConfigProperties.getMaxDisplayedTags()))
                    .operationsSorter(OperationsSorter.ALPHA)
                    .showExtensions(Boolean.valueOf(swaggerConfigProperties.getShowExtensions()))
                    .tagsSorter(TagsSorter.ALPHA).supportedSubmitMethods(UiConfiguration.Constants.DEFAULT_SUBMIT_METHODS)
                    .validatorUrl(null).build();
        }
    
        private ApiInfo apiInfo(SwaggerConfigProperties swaggerConfigProperties) {
            return new ApiInfoBuilder().title(swaggerConfigProperties.getTitle())
                    .description(swaggerConfigProperties.getDescription()).version(swaggerConfigProperties.getApiVersion())
                    .build();
        }
    }
    

    and below are the swagger dependencies

    <properties>
        <java.version>1.8</java.version>
        <swagger.version>2.9.2</swagger.version>
        <swagger-annotations.version>1.5.21</swagger-annotations.version>
        <swagger-models.version>1.5.21</swagger-models.version>
        <spring-plugin.version>2.0.0.BUILD-SNAPSHOT</spring-plugin.version>
    </properties>
    
    
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>${swagger.version}</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>${swagger.version}</version>
    </dependency>
    <dependency>
        <groupId>io.swagger</groupId>
        <artifactId>swagger-annotations</artifactId>
        <version>${swagger-annotations.version}</version>
    </dependency>
    <dependency>
        <groupId>io.swagger</groupId>
        <artifactId>swagger-models</artifactId>
        <version>${swagger-models.version}</version>
    </dependency>
    
    0 讨论(0)
  • 2020-12-09 16:05

    try this version 2.6.1,i already solve with this way

    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger2</artifactId>
        <version>2.6.1</version>
    </dependency>
    <dependency>
        <groupId>io.springfox</groupId>
        <artifactId>springfox-swagger-ui</artifactId>
        <version>2.6.1</version>
    </dependency>
    
    0 讨论(0)
  • 2020-12-09 16:05

    So I actually wanted hateoas support and had the same issue. Turned out this happens if you have

    <dependency>
      <groupId>org.springframework.hateoas</groupId>
      <artifactId>spring-hateoas</artifactId>
    </dependency>
    

    instead of

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-hateoas</artifactId>
    </dependency>
    
    0 讨论(0)
  • 2020-12-09 16:05
        Use following 2 dependency to resolve Swagger and Hateoas dependency conflict.
        
        If Spring Boot version is >= 2.2.0 the use io.springfox version 3.0.0
        
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>3.0.0</version>
        </dependency>
        
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>3.0.0</version>
        </dependency>
    

    After using version 3.0.0 swagger-ui will not be working so use following dependency and user /swagger-ui/ instead /swagger-ui.html

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>
    
    0 讨论(0)
  • 2020-12-09 16:06

    I was getting error

    "Error creating bean with name 'halLinkDisocoverer' defined in class path resource [org/springframework/hateoas/mediatype/hal/HalMediaTypeConfiguration.class]"..

    While Building a Hypermedia-Driven RESTful Web Service

    Deletion of this dependency

    <dependency>
         <groupId>com.jayway.jsonpath</groupId>
         <artifactId>json-path</artifactId>
         <scope>test</scope>
    </dependency>
    

    resolved my issue.

    Check this link for more details Why i am getting an error Factory method 'halLinkDisocoverer' threw exception in springboot?

    0 讨论(0)
  • 2020-12-09 16:14

    The issue faced with me when i using

    <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-dependencies</artifactId>
       <version>2.2.6.RELEASE</version>
       <type>pom</type>
       <scope>import</scope>
    </dependency>
    .....
    .....
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-hateoas</artifactId>
    </dependency>
    

    with springfox swagger

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

    if you take a look into spring hateoas dependencies there is a dependency on spring-plugin-core with version 2.0.0.RELEASE

    <dependency>
        <groupId>org.springframework.plugin</groupId>
        <artifactId>spring-plugin-core</artifactId>
        <version>${spring-plugin.version}</version>
    </dependency> 
    

    but swagger dependency use spring-plugin-core with version 1.2.0.RELEASE.

    spring-boot have conflict on bean creation so, you need to unify the org.springframework.plugin version to make spring see it, If you choose 2.0.0.RELEASE swagger will bot be able to compile,

    so version 1.2.0.RELEASE will be work for both dependencies, like

     <dependency>
        <groupId>org.springframework.plugin</groupId>
        <artifactId>spring-plugin-core</artifactId>
        <version>1.2.0.RELEASE</version>
    </dependency> 
    

    After that you need configuration class to initiate beans for swagger and hateoas like this:

    
    @EnableSwagger2
    @Configuration
    public class SwaggerConfiguration {
    
        @Primary
        @Bean
        public LinkDiscoverers discoverers() {
            List<LinkDiscoverer> plugins = new ArrayList<>();
            plugins.add(new CollectionJsonLinkDiscoverer());
            return new LinkDiscoverers(SimplePluginRegistry.create(plugins));
        }
    
        @Bean
        public Docket postsApi() {
            return new Docket(DocumentationType.SWAGGER_2)
                    .groupName("{ApplicationName}")
                    .apiInfo(buildApiInfo())
                    .select()
                    .apis(RequestHandlerSelectors.any())
                    .paths(PathSelectors.regex("/.*"))
                    .build();
        }
    
        private ApiInfo buildApiInfo() {
            Contact contact = new Contact("CompanyName", "https://company-domain.com", "mail@company.com");
            return new ApiInfoBuilder()
                    .title(""{ApplicationName}"")
                    .description("API Description")
                    .license("license")
                    .version("1.0")
                    .contact(contact)
                    .licenseUrl("licenseURl")
                    .build();
        }
    }
    
    
    0 讨论(0)
提交回复
热议问题