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

文章介绍了如何通过ContentCachingRequestWrapper解决HTTP请求体在过滤器和全局异常处理中只能读取一次的问题。在Filter中包装请求,然后在全局异常处理类中获取请求体内容,注意使用时需确保请求已实际消费。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

背景: 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.youkuaiyun.com/u013887008/article/details/128338085

Spring Boot中,可以使用`@ExceptionHandler`注解来捕获`@Valid`注解抛出的异常,并返回自定义的错误提示信息。 首先,需要在控制器类中定义一个异常处理方法,并在该方法上添加`@ExceptionHandler`注解。在该注解中,需要指定需要捕获的异常类型。例如: ```java @RestControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler(MethodArgumentNotValidException.class) @ResponseStatus(HttpStatus.BAD_REQUEST) public ErrorResponse handleValidationException(MethodArgumentNotValidException ex) { BindingResult bindingResult = ex.getBindingResult(); List<FieldError> fieldErrors = bindingResult.getFieldErrors(); String errorMsg = fieldErrors.stream().map(FieldError::getDefaultMessage).collect(Collectors.joining(";")); return new ErrorResponse(HttpStatus.BAD_REQUEST.value(), errorMsg); } } ``` 在上面的代码中,`@RestControllerAdvice`注解用于定义全局异常处理类。`@ExceptionHandler(MethodArgumentNotValidException.class)`注解用于指定需要捕获的异常类型,即`@Valid`注解抛出的异常。当该异常被捕获时,会执行`handleValidationException()`方法,并返回自定义的错误提示信息。 在`handleValidationException()`方法中,可以通过`MethodArgumentNotValidException`对象获取校验失败的详细信息,并将其封装成自定义的错误响应体`ErrorResponse`返回给前端。`@ResponseStatus(HttpStatus.BAD_REQUEST)`注解用于指定响应状态码为400,表示请求参数有误。 最后,需要在`ErrorResponse`类中定义错误响应体的格式,例如: ```java public class ErrorResponse { private int code; private String message; public ErrorResponse(int code, String message) { this.code = code; this.message = message; } // 省略getter和setter方法 } ``` 以上就是在Spring Boot中拦截`@Valid`注解抛出的异常并提示的方法。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值