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

标签 spring swagger microservices api-gateway

我已经去了Why does springfox-swagger2 UI tell me "Unable to infer base url."Getting an unexpected result while configuring Swagger with Spring Boot并且根本不使用 Spring Security,并且对于每个服务,我使用 @EnableSwagger2 注释。

我正在关注链接中的教程:https://dzone.com/articles/quick-guide-to-microservices-with-spring-boot-20-e并使用 gateway-service 来运行项目,而不是 proxy-service

enter image description here

网关服务.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;
    }
}

最佳答案

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

关于spring - 无法推断基本 URL。当使用动态 servlet 注册或 API 位于 API 网关后面时,这种情况很常见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54285556/

相关文章:

java - 使用 hibernate 的一对多和一对一关系

java - WFLYEJB0137 : Only session and message-driven beans with bean-managed transaction demarcation are allowed to access UserTransaction

java - 使用 REST、Java Spring 的 Hessian Web 服务

python - Swagger API 以及与 python 请求的交互

微服务中的身份验证和授权

java - 你如何在 Spring 更改Docker容器TZ?

java - 用于 api 响应的 Swagger 文档(类型列表)

json - go-swagger 使用路径参数为路由生成规范

c# - ASP.NET Web Api的事件发布者

apache-kafka - 事件溯源是基于编排的 SAGA 模式的增强模式吗?