interceptor prehandle contentcachingrequestwrapper请求空
Interceptor Prehandle的请求为空。 为什么会发生这种情况?
@Component
public class CustomServletWrappingFilter extends OncePerRequestFilter {
@Override
protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
ContentCachingRequestWrapper wrappingRequest = new ContentCachingRequestWrapper(httpServletRequest);
ContentCachingResponseWrapper wrappingResponse = new ContentCachingResponseWrapper(httpServletResponse);
filterChain.doFilter(wrappingRequest, wrappingResponse);
wrappingResponse.copyBodyToResponse();
}
}
预处理请求是空完
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
if (CallCountUtil.isMimeTypeJson(request)) {
ContentCachingRequestWrapper cachingRequestWrapper = (ContentCachingRequestWrapper) request;
String json = new String(cachingRequestWrapper.getContentAsByteArray());
}
}
完成的请求。
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws IOException {
if (CallCountUtil.isMimeTypeJson(request)) {
ContentCachingRequestWrapper cachingRequestWrapper = (ContentCachingRequestWrapper) request;
String json = new String(cachingRequestWrapper.getContentAsByteArray());
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您阅读您会有答案。
在
prehandle
方法中,请求尚未消耗,因此getContentasbyTearray()
是空的。在phatercompletion
中,请求已被消耗并填充了缓存,因此getContentAsbyTearray()
确实返回值。基本上,您不应该关心它是否是
contentCachingRequestWrapper
,只需从httpservletrequest
读取inputStream
,就像通常一样。这样的东西。
If you read the javadocs you would have the answer.
In the
preHandle
method the request hasn't been consumed yet and thusgetContentAsByteArray()
is empty. In theafterCompletion
the request has been consumed and the cache has been filled and thusgetContentAsByteArray()
does return a value.Basically you shouldn't care if it is a
ContentCachingRequestWrapper
, just read theInputStream
from theHttpServletRequest
as you normally would do.Something like this.