feign.codec.EncodeException: Error converting request body

文章讲述了在Feign调用中遇到编码异常,由于无法将HttpServletRequest和HttpServletResponse转换为请求体。解决方法是移除这些参数,通过RequestHeader和CookieValue手动设置请求头和cookie。

feign调用传入HttpServletRequest request, HttpServletResponse response 报错feign.codec.EncodeException: Error converting request body

这个问题通常是因为在传递HttpServletRequest request, HttpServletResponse response时,Feign编码器无法将它们转换为请求体。

解决方案:

  1. 将HttpServletRequest request, HttpServletResponse response从参数中移除。
  2. 在Feign客户端调用时,手动设置请求头、cookie等信息来替代HttpServletRequest和HttpServletResponse。

示例代码:

FeignClient接口调用的接口,这个接口接收的是HttpServletRequest参数

	@RequestMapping(value = "/getDictItems/{dictCode}", method = RequestMethod.GET)
	public Result<List<DictModel>> getDictItems(@PathVariable("dictCode") String dictCode, @RequestParam(value = "sign",required = false) String sign,HttpServletRequest request) {
		 //业务处理
		return result;
	}

FeignClient接口

通过@RequestHeader注解和@CookieValue注解手动设置了请求头和cookie信息。这样就可以避免使用HttpServletRequest和HttpServletResponse传参导致的错误。

    @GetMapping(value = "/sys/dict/getDictItems/{dictCode}")
    Result<List<DictModel>> getDictItems(@PathVariable("dictCode") String dictCode, @RequestParam(value = "sign",
            required = false) String sign, @RequestHeader("Authorization") String token,
                                         @CookieValue("SESSIONID") String sessionId);

contronller调用FeignClient的接口

controller接收的是HttpServletRequest,但是传给FeignClient接口的是token和sessionid。

@GetMapping(value = "/getDictItems/{dictCode}")
public Result<List<DictModel>> getDictItems(@PathVariable("dictCode") String dictCode,
                                            @RequestParam(value = "sign",required = false) String sign,
                                            @NotNull HttpServletRequest request) {
    
    return ddgSysClient.getDictItems(dictCode,sign,
            request.getHeader("Authorization"),
            request.getRequestedSessionId());
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值