SpringBoot 解决request请求体只能被读一次的问题,在全局异常@RestControllerAdvice ExceptionHandler中打印参数

背景: request.getInputStream()获取请求body里面的内容只能被获取一次,ContentCachingRequestWrapper通过这个类能够解决解决HttpServletRequest inputStream只能读取一次的问题,但是这个类有缺陷(前提必须是doFilter之前不能使用request.getInputStream()方法)

使用场景:
全局异常捕获类中打印异常POST请求的参数。

步骤一: 创建Filter

public class CachingContentFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

        if (request instanceof HttpServletRequest) {
            if (request.getContentType() != null && !request.getContentType().contains(MediaType.MULTIPART_FORM_DATA_VALUE)) {
                ContentCachingRequestWrapper wrapper = new ContentCachingRequestWrapper((HttpServletRequest) request);
                //测试
                String requestBody = getRequestBody(wrapper);
                System.out.println("repeat:" + requestBody);
                chain.doFilter(wrapper, response);
                return;
            }
        }
        chain.doFilter(request, response);
    }
    private String getRequestBody(HttpServletRequest request) {
        String requestBody = "";
        ContentCachingRequestWrapper wrapper = WebUtils.getNativeRequest(request, ContentCachingRequestWrapper.class);
        if (wrapper != null) {
            try {
                requestBody = IOUtils.toString(wrapper.getContentAsByteArray(), wrapper.getCharacterEncoding());
            } catch (IOException e) {
            }
        }
        return requestBody;
    }
}

步骤2: 在全局异常捕获类中使用

@RestControllerAdvice
public class ServiceExceptionHandler {
	/** logger */
    private static final Logger LOGGER = LoggerFactory.getLogger(ServiceExceptionHandler.class);
    
    /**
     * 捕获ServiceException
     *
     * @param e
     * @return
     */
    @ExceptionHandler({com.cl.exam.core.exception.ServiceException.class})
    @ResponseStatus(HttpStatus.OK)
    public ApiRest serviceExceptionHandler(HttpServletRequest req, ServiceException e) {
        LOGGER.error("服务异常!",e);
        printLog(req)
        return new ApiRest(e);
    }

    private void printLog(HttpServletRequest req) {
        LOGGER.error("RequestInfo:uri={}, method={}, params={}, body={}",
                req.getRequestURI(),
                req.getMethod(),
                req.getParameterMap(),
                getRequestBody(req));
    }

    private String getRequestBody(HttpServletRequest request) {
        String requestBody = "";
        ContentCachingRequestWrapper wrapper = WebUtils.getNativeRequest(request, ContentCachingRequestWrapper.class);
        if (wrapper != null) {
            try {
                requestBody = IOUtils.toString(wrapper.getContentAsByteArray(), wrapper.getCharacterEncoding());
            } catch (IOException e) {
            }
        }
        return requestBody;
    }
}

注意事项
该类的使用必须在执行到了Controller层,才能通过缓存获取请求体,通过查看源码实现可以发现,实际缓存请求内容的步骤是在读取request.inputStrem的时候进行的缓存(ContentCachingInputStream的read方法中)。
所以只有请求被实际消费了,才能通过ContentCachingRequestWrappergetContentAsByteArray方法获取到请求体,否则是空的。

解决办法:
1、重写ContentCachingRequestWrapper,增加手动缓存方法。不推荐
2、对Controller层进行aop

参考文章:
https://blog.csdn.net/u013887008/article/details/128338085

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
@RestControllerAdvice 是一个用于全局异常处理和统一返回结果的注解。在 Spring Boot 项目,我们可以通过 @RestControllerAdvice 注解来定义一个全局异常处理类。 使用 @RestControllerAdvice 注解的类可以包含以下几个注解: 1. @ExceptionHandler:用于处理特定异常类型的方法。 2. @InitBinder:用于在控制器初始化 WebDataBinder 的方法。 3. @ModelAttribute:将键值对添加到全局的 Model 。 4. @ResponseBody:将方法的返回值作为响应。 当项目发生异常时,@RestControllerAdvice 注解的类会捕获到异常并根据具异常类型执行相应的处理方法。这样可以实现全局异常处理,避免在每个接口都写异常处理逻辑。 下面是一个简单的示例代码: ```java @RestControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler(Exception.class) public ResponseEntity<String> handleException(Exception e) { // 异常处理逻辑 return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage()); } } ``` 在上面的代码,使用 @ExceptionHandler 注解来定义了一个处理 Exception 类型异常的方法。当项目出现 Exception 类型的异常时,会执行该方法,并返回一个带有异常信息的 ResponseEntity 对象。 注意:@RestControllerAdvice 注解只会扫描被 @Controller 或 @RestController 注解的类。因此,确保你的全局异常处理类被正确扫描并起作用。 希望能帮到你!如有其他问题,请继续提问。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值