application/json类型参数从请求到interceptor再到controller,解决流只能读一次的问题,requestbody

网上说的方式能解决问题, 只是有些人没有能说明哪些关键类的要导入那个jar包的问题
要写filter配置在web.xml里面拦截所有请求,在dofilter里面进行对request请求包装起来
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
ServletRequest requestWrapper = null;
System.out.println(“我是过滤器”);
if(request instanceof HttpServletRequest) {
requestWrapper = new RequestWrapper((HttpServletRequest) request);
}
if(requestWrapper == null) {
chain.doFilter(request, response);
} else {
chain.doFilter(requestWrapper, response);
}
}

interceptor:自己要实现的业务逻辑,只是不能再直接用request了, 只能用
RequestWrapper myRequestWrapper = new RequestWrapper((HttpServletRequest) request);
String body = myRequestWrapper.getBody();
System.out.println(“我是拦截器:”+body);
Map<String, Object> bodyMap = JSONObject.fromObject(body);
inDto.putAll(bodyMap);
Dto inDto = Dtos.newDto();
用以上的方式获取body中的参数了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值