SpringBoot 项目通过 HttpServletRequest 获取 body 内容

@PostMapping("/test")
    public void test(HttpServletRequest httpServletRequest) {
		// 获取内容长度
        int length = httpServletRequest.getContentLength();
        // 新建缓存存放读取数据
        byte[] buffer = new byte[length];
        ServletInputStream in = null;

        try {
            in = httpServletRequest.getInputStream();
            in.read(buffer, 0, length);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != in) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        try {
            log.info(new String(buffer, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
	
    }

版权声明:本文为weixin_43548119原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。