Offer to come, dig friends take it! I am participating in the 2022 Spring Recruit Punch card activity. Click here for details.
I. Problem Description:
In a nutshell: Our FeignClient interface supports POJO parameters for GET requests?
If the interface we call is a GET request, if we define the POJO argument directly with @feignClient:
@FeignClient(name = "payment-service", contextId = "payment-core", path = "/payment")
public interface PaymentFeign {
@GetMapping("/refund")
RefundPaymentVo refund(RefundPaymentDto refundPaymentDto);
}
Copy the code
The following error message is displayed:
feign.FeignException$MethodNotAllowed: [405] during [GET] to [http://payment-service/payment/refund] [PaymentFeign#refund(RefundPaymentDto)]: [{" timestamp ":" the 2022-03-26 T18:34:47. 058 + 00:00 ", "status" : 405, "error" : "Method Not Allowed", "path" : "/ payment/refund"}] the 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(FeignCircuitBreakerInvocati onHandler.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)Copy the code
Ii. Solutions:
The OpenFeign@QueryMap annotation supports the use of POJOs as GET parameter mappings. Unfortunately, the default OpenFeign QueryMap annotation is incompatible with Spring because it lacks the value attribute.
Spring Cloud OpenFeign provides the equivalent @SpringQueryMap annotation for annotating POJO or Map parameters as query parameter mappings.
For example, the RefundPaymentDto class defines the parameters orderCode and refundAmount:
@Data
public class RefundPaymentDto {
/** * Order number */
private String orderCode;
/** * Amount of refund (unit: minute) */
private Integer refundAmount;
}
Copy the code
The following Feign clients use the Params class with annotations: @SpringQueryMap
@FeignClient(name = "payment-service", contextId = "payment-core", path = "/payment")
public interface PaymentFeign {
@GetMapping("/refund")
RefundPaymentVo refund(@SpringQueryMap RefundPaymentDto refundPaymentDto);
}
Copy the code
If you need more control over the generated query parameter mapping, you can implement a custom QueryMapEncoderbean.
Iii. Summary:
The use of @SpringQueryMap annotations simplifies the complex definition of GET parameters in FeignClient (if we can only use @requestParam for a single definition).
1. Spring Cloud OpenFeign official documentation