Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

After Update of Springfox from 2.9.2 to 2.10.4 error "Unable to infer base url. ..."

I just updated the Springfox dependency in my Spring Boot application (version 2.3.1.RELEASE) from 2.9.2 to 2.10.4.

<spring-boot.version>2.3.1.RELEASE</spring-boot.version>
<swagger.version>2.10.4</swagger.version>

Due to class changes in the springfox.documentation.* packages, I had to change the annotation in my configuration class from

@EnableSwagger2

to

@EnableSwagger2WebMvc

Also the Predicate import changed from Google Guave to java.util.function. My current Configuration class looks like this

package de.rewe.zlip.config;

import java.util.LinkedList;
import java.util.List;
import java.util.Optional;
import java.util.function.Predicate;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import springfox.documentation.RequestHandler;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

@Configuration
@EnableSwagger2WebMvc
public class SwaggerConfig {

    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2).globalOperationParameters(globalOperationParameters())
                                                      .select()
                                                      .apis(sourceScannedForRestApis())
                                                      .paths(PathSelectors.any())
                                                      .build()
                                                      .apiInfo(apiEndPointsInfo())
                                                      .genericModelSubstitutes(Optional.class);
    }

    private List<Parameter> globalOperationParameters() {
        List<Parameter> operationParameters = new LinkedList<>();
        Parameter authorizationParameter = new ParameterBuilder().name("Authorization")
                                                                 .description("Your Bearer token ")
                                                                 .modelRef(new ModelRef("string"))
                                                                 .parameterType("header")
                                                                 .build();
        operationParameters.add(authorizationParameter);
        return operationParameters;
    }

    private Predicate<RequestHandler> sourceScannedForRestApis() {
        return RequestHandlerSelectors.basePackage("de.my.package");
    }

    private ApiInfo apiEndPointsInfo() {
        return new ApiInfoBuilder().title("TEST SERVER REST API")
                                   .description("REST API provided for the TEST web application")
                                   .contact(contactInfo())
                                   .version("v1.0")
                                   .build();
    }

    private Contact contactInfo() {
        return new Contact("Test Team", "https://", "[email protected]");
    }

}

When I open http://localhost:8080/swagger-ui.html now, I get the following message:

Unable to infer base url. This is common when using dynamic servlet registration or when the API is behind an API Gateway. The base url is the root of where all the swagger resources are served. For e.g. if the api is available at http://example.org/api/v2/api-docs then the base url is http://example.org/api/. Please enter the location manually:

Needless to say, the same configuration (except of the 2 changes mentioned above) worked with 2.9.2. Most of the tips in earlier questions are adding

@EnableSwagger2

but since this annotation has changed in 2.10.X to either @EnableSwagger2Mvc or @EnableSwagger2Flux this won't help.

like image 628
Thorsten Kitz Avatar asked Jun 25 '20 10:06

Thorsten Kitz


2 Answers

From springfox issue tracker:

Oh please dont use 2.10... it was an intermediate step so that people who were on 3.0.0-SNAPSHOT can continue using a released version if needed.

I suggest reverting back to 2.9.2 for the time being.

like image 101
didrikjonassen Avatar answered Sep 19 '22 16:09

didrikjonassen


You can try below.

  1. Add this dependency to your pom.xml
<dependency>
  <groupId>io.springfox</groupId>
  <artifactId>springfox-data-rest</artifactId>
  <version>${your.spring.fox.version}</version>
</dependency>
  1. Add this line to either your main class or your swagger config class
@Import(SpringDataRestConfiguration.class)
like image 20
deiyanfott Avatar answered Sep 21 '22 16:09

deiyanfott