Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/344.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 尝试登录HandlerInterceptor时请求正文为空_Java_Servlets_Httprequest_Servlet Filters - Fatal编程技术网

Java 尝试登录HandlerInterceptor时请求正文为空

Java 尝试登录HandlerInterceptor时请求正文为空,java,servlets,httprequest,servlet-filters,Java,Servlets,Httprequest,Servlet Filters,我试图从HttpServletRequest记录请求正文,为此我添加了一个包装器,在检索到请求正文后,仍然会得到一个空的请求正文。请问我怎样才能多次拿到尸体 public class LogginHandlerInterceptor implements HandlerInterceptor { @Override public boolean preHandle(HttpServletRequest request, HttpServle

我试图从HttpServletRequest记录请求正文,为此我添加了一个包装器,在检索到请求正文后,仍然会得到一个空的请求正文。请问我怎样才能多次拿到尸体

public class LogginHandlerInterceptor implements HandlerInterceptor {   
         @Override
                public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
                        throws Exception {
                    System.out.println("preHandle - " + handler);
                    MDC.put(START_TIME_VAR, Long.toString(System.currentTimeMillis()));
                    MultiReadHttpServletRequest requestWrapper = new MultiReadHttpServletRequest(request);

                final StringBuilder builder = new StringBuilder();
                BufferedReader reader = null;
                InputStream inputStream = requestWrapper.getInputStream();
                if (inputStream != null) {
                    reader = new BufferedReader(new InputStreamReader(inputStream));
                    String line;
                    while ((line = reader.readLine()) != null) {
                        builder.append(line);
                    }

                    MDC.put(REQUEST_BODY_VAR, builder.toString());
包装器:

public class MultiReadHttpServletRequest extends HttpServletRequestWrapper {
    private ByteArrayOutputStream cachedBytes;

    public MultiReadHttpServletRequest(HttpServletRequest request) {
        super(request);
    }

    @Override
    public ServletInputStream getInputStream() throws IOException {
        if (cachedBytes == null)
            cacheInputStream();

        return new CachedServletInputStream();
    }

    @Override
    public BufferedReader getReader() throws IOException {
        return new BufferedReader(new InputStreamReader(getInputStream()));
    }

    private void cacheInputStream() throws IOException {
        /*
         * Cache the inputstream in order to read it multiple times. For
         * convenience, I use apache.commons IOUtils
         */
        cachedBytes = new ByteArrayOutputStream();
        IOUtils.copy(super.getInputStream(), cachedBytes);
    }

    /* An inputstream which reads the cached request body */
    public class CachedServletInputStream extends ServletInputStream {
        private ByteArrayInputStream input;

        public CachedServletInputStream() {
            /* create a new input stream from the cached request body */
            input = new ByteArrayInputStream(cachedBytes.toByteArray());
        }

        @Override
        public int read() throws IOException {
            return input.read();
        }
    }
}
其中我得到一个空的请求正文(rawrequest有一个头,但正文为空):

@RequestMapping(value=“/someuri”,method=RequestMethod.POST,consumes=MediaType.APPLICATION\u JSON\u value,products=MediaType.APPLICATION\u JSON\u value)
public@ResponseBody对象post(最终HttpServletRequest请求,
@PathVariable最终字符串参数,@PathVariable最终字符串参数2,
HttpEntity rawrequest,@RequestHeader(“授权”)最终字符串授权){

使用
ContentCachingRequestWrapper
而不是
MultiReadHttpServletRequest

HttpServletRequest requestCacheWrapperObject = new ContentCachingRequestWrapper(request);
requestCacheWrapperObject.getParameterMap();
// Read inputStream from requestCacheWrapperObject and log it

更多信息。

我在拦截器中使用setAttribute找到了重置请求正文的解决方案

request.setAttribute("com.xp.input", body);
然后在我的过程中检索:

Object body = request.getAttribute("com.xp.input");

ContentCachingRequestWrapper类仅支持以下内容:内容类型:application/x-www-form-urlencoded方法类型:POST,我正在使用application/json
Object body = request.getAttribute("com.xp.input");