无法推断基本网址。当使用动态 Servlet 注册或 API 位于 API 网关后面时,这种情况很常见

问题描述 投票:0回答:3

我已经去了 为什么 springfox-swagger2 UI 告诉我“无法推断基本 url。”使用 Spring Boot 配置 Swagger 时得到意外结果并且根本不使用 Spring Security 对于每个服务,我正在使用

@EnableSwagger2
注释。

我正在关注链接中的教程:https://dzone.com/articles/quick-guide-to-microservices-with-spring-boot-20-e并使用

gateway-service
来运行项目而不是
proxy-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 和所有其他服务都是这样实现的。

@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;
    }
}
spring swagger microservices api-gateway
3个回答
0
投票

将 springfox-swagger2 和 springfox-swagger-ui 依赖项升级到 2.9.2 版本。


0
投票

确保您的 pom.xml 中有以下 3 个依赖项

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

注意:上述 3 个依赖项的 springfox 版本应该相同,否则可能会出现其他错误

SpringDataRestConfiguration 类导入到您的 OrganizationApplication 类,如下所示:

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

0
投票

对我来说,当我为 DTO 添加换行方面时,出现此错误,而当我为非我的 DTO 添加跳过换行时,该错误就会消失,例如

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);
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.