简体   繁体   English

在 Spring Boot Rest Controller 中处理压缩的 json 请求

[英]Process compressed json request in Spring Boot Rest Controller

I've been trying to process the RequestBody which is JSON data sent as GZIP.我一直在尝试处理 RequestBody,它是作为 GZIP 发送的 JSON 数据。 In my RestController I have the following method:在我的 RestController 中,我有以下方法:

@RequestMapping(value = "/api/data", method = RequestMethod.POST)
public ResponseEntity<String> updateData(@RequestBody String data) {
    System.out.println(data);

    return new ResponseEntity(HttpStatus.CREATED);
}

The 'data' which is sent to the controller is JSON that is GZIP-ed and when I print it, it is all encoded.发送到控制器的“数据”是 GZIP 格式的 JSON,当我打印它时,它都是经过编码的。 How can I decode/unzip this data?如何解码/解压缩此数据?

In the end from different sources I scraped the answer.最后,我从不同的来源抓取了答案。 Here's an example:下面是一个例子:

@Component
public class GzipBodyDecompressFilter implements Filter {

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain)
            throws IOException, ServletException {
            
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;

        request = new GzippedInputStreamWrapper((HttpServletRequest) servletRequest);

        chain.doFilter(request, response);
    }

    final class GzippedInputStreamWrapper extends HttpServletRequestWrapper {

        public static final String DEFAULT_ENCODING = "ISO-8859-1";

        private byte[] bytes;

        public GzippedInputStreamWrapper(final HttpServletRequest request) throws IOException {
            super(request);
            try {
                final InputStream in = new GZIPInputStream(request.getInputStream());
                bytes = IOUtils.toByteArray(in);
            } catch (EOFException e) {
                bytes = new byte[0];
            }
        }

        @Override
        public ServletInputStream getInputStream() throws IOException {
            final ByteArrayInputStream sourceStream = new ByteArrayInputStream(bytes);
            return new ServletInputStream() {
                public int read() throws IOException {
                    return sourceStream.read();
                }

                @Override
                public void setReadListener(ReadListener listener) {
                    // TODO Auto-generated method stub

                }

                @Override
                public boolean isFinished() {
                    // TODO Auto-generated method stub
                    return false;
                }

                @Override
                public boolean isReady() {
                    // TODO Auto-generated method stub
                    return false;
                }

                public void close() throws IOException {
                    super.close();
                    sourceStream.close();
                }
            };
        }

        @Override
        public Map getParameterMap() {
            return super.getParameterMap();
        }
    }
}

doFilter method will be executed before it reaches controller, in which I can do anything to the body of the request (since I have access to ServletRequest), eg creating an implementation for HttpServletRequestWrapper where I unzip the content. doFilter方法将在它到达控制器之前执行,在该方法中我可以对请求的主体执行任何操作(因为我可以访问 ServletRequest),例如为HttpServletRequestWrapper创建一个实现,在其中解压缩内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM