在网关ZuulFilter内,做了加解密,即RequestContext重设HttpServletRequest之后,业务请求参数的长度会发生变化。如果长度变长,那么真正走到业务方法内的时候,就会导致入参读取不全报错,需要在RequestContext重设HttpServletRequestWrapper的时候,重写HttpServletRequestWrapper的长度获取方法。
关键代码如下:
//重设数据长度,使得替换数据后不会造成接收方接收不全 @Override int getContentLength() { return reqBodyBytes.length; } @Override long getContentLengthLong() { return reqBodyBytes.length; }
@Override
Object run() {
RequestContext ctx = RequestContext.getCurrentContext()
HttpServletRequest request = ctx.getRequest()
InputStream inStream = request.getInputStream()
String requestData = StreamUtils.copyToString(inStream, StandardCharsets.UTF_8)
logger.info("ZuulFilter test ,url is {},method is {}, requestData is {}"