Offer arrives, dig friends to pick up! I am participating in the 2022 Spring Recruitment Check-In Event, click to view the event details .
1. Problem description:
In one sentence: Does our FeignClient interface support POJO parameters under GET requests?
If the interface we call is a GET request, if we use @FeignClient to directly define POJO parameters, the code is as follows:
@FeignClient(name = "payment-service", contextId = "payment-core", path = "/payment")
public interface PaymentFeign {
@GetMapping("/refund")
RefundPaymentVo refund(RefundPaymentDto refundPaymentDto);
}
复制代码
The following error message will be reported:
feign.FeignException$MethodNotAllowed: [405] during [GET] to [http://payment-service/payment/refund] [PaymentFeign#refund(RefundPaymentDto)]: [{"timestamp":"2022-03-26T18:34:47.058+00:00","status":405,"error":"Method Not Allowed","path":"/payment/refund"}]
at feign.FeignException.clientErrorStatus(FeignException.java:221)
at feign.FeignException.errorStatus(FeignException.java:194)
at feign.FeignException.errorStatus(FeignException.java:185)
at feign.codec.ErrorDecoder$Default.decode(ErrorDecoder.java:92)
at feign.AsyncResponseHandler.handleResponse(AsyncResponseHandler.java:96)
at feign.SynchronousMethodHandler.executeAndDecode(SynchronousMethodHandler.java:138)
at feign.SynchronousMethodHandler.invoke(SynchronousMethodHandler.java:89)
at org.springframework.cloud.openfeign.FeignCircuitBreakerInvocationHandler.lambda$asSupplier$1(FeignCircuitBreakerInvocationHandler.java:112)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
复制代码
Second, the solution:
OpenFeign @QueryMap
annotations support using POJOs as GET parameter mappings. Unfortunately, the default OpenFeign QueryMap annotation is not compatible with Spring because it lacks value
properties.
Spring Cloud OpenFeign provides equivalent @SpringQueryMap
annotations for annotating POJO or Map parameters as query parameter mappings.
For example, RefundPaymentDto
the class defines parameters orderCode
and refundAmount
:
@Data
public class RefundPaymentDto {
/**
* 订单号
*/
private String orderCode;
/**
* 退款金额(单位:分)
*/
private Integer refundAmount;
}
复制代码
The following feign clients use Params
this class via the annotation:@SpringQueryMap
@FeignClient(name = "payment-service", contextId = "payment-core", path = "/payment")
public interface PaymentFeign {
@GetMapping("/refund")
RefundPaymentVo refund(@SpringQueryMap RefundPaymentDto refundPaymentDto);
}
复制代码
If you need more control over the generated query parameter mapping, you can implement a custom QueryMapEncoder
bean.
3. Summary:
@SpringQueryMap
The use of annotations simplifies our complex definition of GET parameters in FeignClient (if we can only use a single definition @RequestParam
)
References: 1. Spring Cloud OpenFeign official documentation