【问题标题】:Feign client sub-domain : ambiguous mappingFeign客户端子域:模糊映射
【发布时间】:2019-03-16 11:29:19
【问题描述】:

我正在使用 Spring Boot 2.0.3.RELEASE 和 openFeign:

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

我在我的项目中声明了两个假客户:

@FeignClient(name = "appleReceiptSandboxFeignClient",
    url = "https://sandbox.itunes.apple.com",
    configuration = Conf.class)
@RequestMapping(produces = "application/json", consumes = "application/json")
public interface AppleReceiptSandboxFeignClient {

    @RequestMapping(value = "/verifyReceipt", method = RequestMethod.POST)
    AppleReceiptResponseDTO sandboxVerifyReceipt(@RequestBody AppleReceiptRequestDTO dto);

}
@FeignClient(name = "appleReceiptFeignClient",
    url = "https://buy.itunes.apple.com")
@RequestMapping(produces = "application/json", consumes = "application/json")
public interface AppleReceiptFeignClient {

    @RequestMapping(value = "/verifyReceipt", method = RequestMethod.POST)
    AppleReceiptResponseDTO productionVerifyReceipt(@RequestBody AppleReceiptRequestDTO dto);

}

我的问题是,即使base urls和name不一样,似乎feign客户端也被认为是冲突的。

java.lang.IllegalStateException: Ambiguous mapping. Cannot map 'com.myproject.central.client.AppleReceiptSandboxFeignClient' method 
public abstract com.myproject.central.client.dto.AppleReceiptResponseDTO com.myproject.central.client.AppleReceiptSandboxFeignClient.sandboxVerifyReceipt(com.myproject.central.client.dto.AppleReceiptRequestDTO)
to {[/verifyReceipt],methods=[POST],consumes=[application/json],produces=[application/json]}: There is already 'com.myproject.central.client.AppleReceiptFeignClient' bean method
public abstract com.myproject.central.client.dto.AppleReceiptResponseDTO com.myproject.central.client.AppleReceiptFeignClient.productionVerifyReceipt(com.myproject.central.client.dto.AppleReceiptRequestDTO) mapped.

即使这个映射错误是意料之外的,我也乐于解决问题。

我显然不能在同一个 feign 客户端中声明两个端点,因为子域不同,或者我错过了什么?

我的问题是:仅使用 feign 客户端最简单的解决方法(如果有的话)是什么?

【问题讨论】:

    标签: spring-boot spring-cloud-feign


    【解决方案1】:

    当您使用@RequestMapping 注释接口或类时,即使您有@FeignClient 注释,Spring 也会注册一个处理程序。您可以通过从接口中删除注释并仅在方法上使用它来解决此问题。

    @FeignClient(name = "appleReceiptSandboxFeignClient",
            url = "https://sandbox.itunes.apple.com",
            configuration = Conf.class)
    public interface AppleReceiptSandboxFeignClient {
    
        @RequestMapping(value = "/verifyReceipt", method = RequestMethod.POST, produces = "application/json", consumes = "application/json", pro)
        AppleReceiptResponseDTO sandboxVerifyReceipt(@RequestBody AppleReceiptRequestDTO dto);
    
    }
    
    @FeignClient(name = "appleReceiptFeignClient",
            url = "https://buy.itunes.apple.com")
    public interface AppleReceiptFeignClient {
    
        @RequestMapping(value = "/verifyReceipt", method = RequestMethod.POST, produces = "application/json", consumes = "application/json")
        AppleReceiptResponseDTO productionVerifyReceipt(@RequestBody AppleReceiptRequestDTO dto);
    
    }
    

    【讨论】:

      猜你喜欢
      • 2018-07-22
      • 1970-01-01
      • 2021-09-16
      • 2019-11-04
      • 1970-01-01
      • 2020-12-20
      • 2020-09-10
      • 2012-07-19
      • 1970-01-01
      相关资源
      最近更新 更多