伪客户端子域:模棱两可的映射

时间:2019-03-16 11:29:20

标签: spring-boot spring-cloud-feign

我正在使用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);

}

我的问题是,即使基本网址和名称不同,似乎伪客户端也被认为是冲突的。

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.

即使此映射错误是意外的,我也可以采用解决方法。

我显然不能在同一个伪客户端中声明两个端点,因为子域不同,或者我丢失了某些东西吗?

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

1 个答案:

答案 0 :(得分: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);

}