如何在 FeignClient 中定义具有多个端点的映射?

How to define mapping with multiple endpoints in FeignClient?

我在我的应用程序中创建了一个 @FeignCleint,具有以下映射:

@FeignClient(name="${mongo.service.id}", url="${mongo.service.url}")
public interface MongoBusinessDayDataInterface {
    String requestMappingPrefix = "/api/businessDayData";

    @GetMapping(path = {requestMappingPrefix + "/{businessDate}",
                        requestMappingPrefix + "/{businessDate}/{terminalId}",
                        requestMappingPrefix + "/{businessDate}/{currency}",
                        requestMappingPrefix + "/{businessDate}/{terminalId}/{currency}"})
    BusinessDayData fetchBusinessDayData(@PathVariable @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate businessDate,
                                         @PathVariable(required = false) String terminalId,
                                         @PathVariable(required = false) CurrencyType currency);
}

当我尝试加载应用程序时出现以下异常:

org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'com.poalim.dc.atm.reconciliation.rest.feign.service.MongoBusinessDayDataInterface': 
FactoryBean threw exception on object creation; 
nested exception is java.lang.IllegalStateException: 
Method fetchBusinessDayDataList can only contain at most 1 value field. 
Found: [/api/businessDayData/list/{businessDate}, /api/businessDayData/list/{businessDate}/{terminalId}, /api/businessDayData/list/{businessDate}/{currency}, /api/businessDayData/list/{businessDate}/{terminalId}/{currency}]

如果路径属性可以接收字符串数组,为什么会这样说"can only contain at most 1 value field"?

@GetMapping 是来自Spring Web 模块的注释,常用于服务器应用程序的控制器。

默认情况下,Feign 不支持此类注解。它有 its own set of them。例如,@RequestLine 仅支持一种路径。

spring-cloud-openfeign 添加了额外的支持以使用 spring-web 的注释来伪装客户端,纯粹是为了开发人员的方便(请参阅 SpringMvcContract)。但是,在这种情况下,有一些注释的签名不匹配,在这种情况下您应该忽略它们。

顺便说一句,使用Feign时设置多个路径在逻辑上是没有意义的。它是一个客户端,所以它应该知道当你通过它发出请求时会选择哪条路径。