网上说的方式能解决问题, 只是有些人没有能说明哪些关键类的要导入那个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中的参数了。