Spring REST service: retrieving JSON from Request

后端 未结 11 682
甜味超标
甜味超标 2020-11-27 04:35

I am building a REST service on Spring 3.1. I am using @EnableWebMVC annotation for that. Since my service will only be accepting JSON requests, I would also like to dump th

11条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-27 04:43

    I make a Ouputstream version without any dependency to 3rd party libs for easier re-use. You can use this 2 wrapper class to get the request & response body easily.
    But anyway, I have to use a filter to do this instead of interceptor. Because as @user1323865 mentioned, in spring 4, the processedRequest is used in both interceptor and handler, so you cannot use these methods for interceptor.
    Also you can find some help in this link if you're using Writer version instead. Capture and log the response body

    
    public class BufferedRequestWrapper extends HttpServletRequestWrapper
    {
        private static final class BufferedServletInputStream extends ServletInputStream
        {
            private ByteArrayInputStream bais;
    
            public BufferedServletInputStream(ByteArrayInputStream bais)
            {
                this.bais = bais;
            }
    
            @Override
            public int available()
            {
                return this.bais.available();
            }
    
            @Override
            public int read()
            {
                return this.bais.read();
            }
    
            @Override
            public int read(byte[] buf, int off, int len)
            {
                return this.bais.read(buf, off, len);
            }
    
        }
    
        private byte[] mBodyBuffer;
    
        public BufferedRequestWrapper(HttpServletRequest request) throws IOException
        {
            super(request);
    
            InputStream in = request.getInputStream();
    
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int bytesRead = -1;
            while ((bytesRead = in.read(buffer)) > 0)
            {
                baos.write(buffer, 0, bytesRead);
            }
            mBodyBuffer = baos.toByteArray();
        }
    
        public String getRequestBody()
        {
            return new String(mBodyBuffer, Charset.forName("UTF-8"));
        }
        @Override
        public BufferedReader getReader() throws IOException
        {
            return new BufferedReader(new InputStreamReader(this.getInputStream()));
        }
    
        @Override
        public ServletInputStream getInputStream()
        {
            ByteArrayInputStream in = new ByteArrayInputStream(mBodyBuffer);
            return new BufferedServletInputStream(in);
        }
    }
    
    
    public class BufferedResponseWrapper extends HttpServletResponseWrapper
    {
        private TeeServletOutputStream mTeeOutputStream;
    
        private static class TeeOutputStream extends OutputStream
        {
            private OutputStream mChainStream;
            private OutputStream mTeeStream;
    
            public TeeOutputStream(OutputStream chainStream, OutputStream teeStream)
            {
                mChainStream = chainStream;
                mTeeStream = teeStream;
            }
    
            @Override
            public void write(int b) throws IOException
            {
                mChainStream.write(b);
                mTeeStream.write(b);
                mTeeStream.flush();
            }
    
            @Override
            public void close() throws IOException
            {
                flush();
                mChainStream.close();
                mTeeStream.close();
            }
    
            @Override
            public void flush() throws IOException
            {
                mChainStream.close();
            }
        }
    
        public class TeeServletOutputStream extends ServletOutputStream
        {
            private final TeeOutputStream targetStream;
    
            public TeeServletOutputStream(OutputStream one, OutputStream two)
            {
                targetStream = new TeeOutputStream(one, two);
            }
    
            @Override
            public void write(int b) throws IOException
            {
                this.targetStream.write(b);
            }
    
            @Override
            public void flush() throws IOException
            {
                super.flush();
                this.targetStream.flush();
            }
    
            @Override
            public void close() throws IOException
            {
                super.close();
                this.targetStream.close();
            }
        }
    
        private ByteArrayOutputStream mByteArrayOutputStream;
    
        public BufferedResponseWrapper(HttpServletResponse response) throws IOException
        {
            super(response);
            mByteArrayOutputStream = new ByteArrayOutputStream();
            mTeeOutputStream = new TeeServletOutputStream(super.getResponse().getOutputStream(), mByteArrayOutputStream);
        }
    
        @Override
        public PrintWriter getWriter() throws IOException
        {
            return super.getResponse().getWriter();
        }
    
        @Override
        public ServletOutputStream getOutputStream() throws IOException
        {
            return mTeeOutputStream;
        }
    
        public String getResponseBody()
        {
            return mByteArrayOutputStream.toString();
        }
    
    }
    

提交回复
热议问题