6

I already went Why does springfox-swagger2 UI tell me "Unable to infer base url." and Getting an unexpected result while configuring Swagger with Spring Boot and not using Spring Security at all and for each service, I am using @EnableSwagger2 annotations.

I'm following tutorial from link: https://dzone.com/articles/quick-guide-to-microservices-with-spring-boot-20-e and using gateway-service for the project to run instead of proxy-service.

enter image description here

gateway-service.yml

server:
  port: 8060

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8061/eureka/

logging:
  pattern: 
    console: "%d{yyyy-MM-dd HH:mm:ss} ${LOG_LEVEL_PATTERN:-%5p} %m%n"

spring:
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true
      routes:
      - id: employee-service
        uri: lb://employee-service
        predicates:
        - Path=/employee/**
        filters:
        - RewritePath=/employee/(?<path>.*), /$\{path}
      - id: department-service
        uri: lb://department-service
        predicates:
        - Path=/department/**
        filters:
        - RewritePath=/department/(?<path>.*), /$\{path}
      - id: organization-service
        uri: lb://organization-service
        predicates:
        - Path=/organization/**
        filters:
        - RewritePath=/organization/(?<path>.*), /$\{path}

OrganizationApplication.java and all other services are implemented exactly like this.

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableSwagger2
public class OrganizationApplication {

    public static void main(String[] args) {
        SpringApplication.run(OrganizationApplication.class, args);
    }

    @Bean
    public Docket swaggerApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                    .apis(RequestHandlerSelectors.basePackage("pl.piomin.services.organization.controller"))
                    .paths(PathSelectors.any())
                .build()
                .apiInfo(new ApiInfoBuilder().version("1.0").title("Organization API").description("Documentation Organization API v1.0").build());
    }

    @Bean
    OrganizationRepository repository() {
        OrganizationRepository repository = new OrganizationRepository();
        repository.add(new Organization("Microsoft", "Redmond, Washington, USA"));
        repository.add(new Organization("Oracle", "Redwood City, California, USA"));    
        return repository;
    }
}
Jeff Cook
  • 7,956
  • 36
  • 115
  • 186

3 Answers3

0

Upgrade your springfox-swagger2 and springfox-swagger-ui dependencies to 2.9.2 version.

0

Ensure you have below 3 dependency in your pom.xml

        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>{your.spring.fox.version}</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>{your.spring.fox.version}</version>
        </dependency>    
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-data-rest</artifactId>
            <version>{your.spring.fox.version}</version>
        </dependency>

NB: springfox version should be same on the above 3 dependencies otherwise you might get other errors

Import the SpringDataRestConfiguration class to your OrganizationApplication class as shown below:

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
@EnableSwagger2
@Import(SpringDataRestConfiguration.class)
public class OrganizationApplication {

    public static void main(String[] args) {
        SpringApplication.run(OrganizationApplication.class, args);
    }

     // you other code to go here
     -----
 }
Ole S
  • 76
  • 7
0

For me, I get this error was when I added wrapping aspect for DTOs and it goes out when I add skipping of wrapping for not my DTOs, for example springfox.documentation.swagger.web.UiConfiguration:

package ru.nashev.try2.controller;

import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
import ru.nashev.try2.dto.ResultDataDTO;
import ru.nashev.try2.dto.ResultErrorDTO;
import ru.nashev.try2.dto.ResultSuccessDTO;

@RestControllerAdvice
public class ResponseBodyHandler implements ResponseBodyAdvice<Object> {

    @Override
    public boolean supports(MethodParameter methodParameter, Class<? extends HttpMessageConverter<?>> aClass) {
        return true;
    }

    @Override
    public Object beforeBodyWrite(Object body, MethodParameter methodParameter, MediaType mediaType, Class<? extends HttpMessageConverter<?>> aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse response) {
        if (body == null) {
            return new ResultDataDTO(new ResultSuccessDTO());
        } else if (!body.getClass().getName().startsWith("ru.nashev.try2.dto")) { // skip swagger classes etc
            return body;
        } else if (body instanceof ResultErrorDTO) {
            return body;
        } else {
            return new ResultDataDTO(body);
        }
    }
}
Nashev
  • 490
  • 4
  • 10