FeignClient 调用服务端时,服务端报错:Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method ‘POST’ not supported]
FeignClient 端
@GetMapping("/purchasingOrder/selectAllPurchasingOrderForJointQuery")
CommonResult<PageInfo<PurchasingOrder>> selectAllPurchasingOrderForJointQuery(
@RequestParam("page") Integer page,
@RequestParam("pageSize") Integer pageSize,
@RequestBody PurchasingOrder purchasingOrder);
被调用服务端
@GetMapping("/purchasingOrder/selectAllPurchasingOrderForJointQuery")
public CommonResult<PageInfo<PurchasingOrder>> selectAllPurchasingOrderForJointQuery(
@RequestParam("page") Integer page,
@RequestParam("pageSize") Integer pageSize,
@RequestBody PurchasingOrder purchasingOrder){
return purchasingOrderService.selectAllPurchasingOrderForJointQuery(page, pageSize, purchasingOrder);
}
报错:
Resolved [org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'POST' not supported]
说不支持Post请求,但是我FeignClient 端用的是Get请求,服务端用的也是Get请求,为什么会报错不支持Post请求呢?
而我把客户端和服务端都改成Post请求后,就解决问题了。
原因:
因为我FeignClient 端的请求参数是@RequestBody PurchasingOrder purchasingOrder,当把对象转换为json字符串的时候 FeignClient会默认发送Post请求
解决方法
1、如果使用@RequestBody 注解,把客户端与服务端都改为Post请求
2、如果非要用Get传送对象类型参数,可以使用@SpringQueryMap注解,前提是的springboot 2.2版本以上,它的作用是直接以对象的形式传参,服务端可直接用以下方式接收参数:
@GetMapping("/purchasingOrder/selectAllPurchasingOrderForJointQuery")
public CommonResult<PageInfo<PurchasingOrder>> selectAllPurchasingOrderForJointQuery(
@RequestParam("page") Integer page,
@RequestParam("pageSize") Integer pageSize,
PurchasingOrder purchasingOrder){
return purchasingOrderService.selectAllPurchasingOrderForJointQuery(page, pageSize, purchasingOrder);
}