简体   繁体   English

如何从 Spring Boot 的 ExceptionHandler 中的请求中获取正文数据?

[英]How can I get a body data from the request in an ExceptionHandler in Spring Boot?

I have a @RestControllerAdvice where I handle an Exception in Spring Boot.我有一个 @RestControllerAdvice,我在其中处理 Spring Boot 中的异常。 I would like to log an information that is sent through request body.我想记录通过请求正文发送的信息。 How can I get this information from a spring WebRequest?如何从 spring WebRequest 中获取此信息?

This is my sample exception handler.这是我的示例异常处理程序。

@RestControllerAdvice
public class CustomExceptionHandler extends ResponseEntityExceptionHandler {

@Override
protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex,
        HttpHeaders headers, HttpStatus status, WebRequest request) {

    // I want to add something here that I could log an info that is in the request body.
    return super.handleMethodArgumentNotValid(ex, headers, status, request);
}

} }

@M.Deinum I tried to use ContentCachingRequestWrapper, But I could not have acess to body content. @M.Deinum 我尝试使用 ContentCachingRequestWrapper,但我无法访问正文内容。 The method contentCachingRequestWrapper.getContentAsByteArray() returns null. contentCachingRequestWrapper.getContentAsByteArray() 方法返回 null。

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

    try {
        ContentCachingRequestWrapper wrappedRequest = new ContentCachingRequestWrapper((HttpServletRequest) request);
        wrappedRequest.getContentAsByteArray();
        wrappedRequest.getInputStream();
        chain.doFilter(wrappedRequest, response);
    } finally {
        LoggingContext.clear();
    }

The comments regarding using the ContentCachingRequestWrapper are accurate, this is the implementation using your controller advice that should work.关于使用ContentCachingRequestWrapper的评论是准确的,这是使用您的 controller 建议的实现应该有效。

@Component
public class MyFilter implements Filter {
  @Override
  public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
  throws IOException, ServletException {
     ContentCachingRequestWrapper contentCachingRequestWrapper = new ContentCachingRequestWrapper(
    (HttpServletRequest) servletRequest);

     filterChain.doFilter(contentCachingRequestWrapper, servletResponse);
  }
}

The advice建议

@RestControllerAdvice
public class CustomExceptionHandler extends ResponseEntityExceptionHandler {

  @Override
  protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers,
  HttpStatus status, WebRequest request) {

    ContentCachingRequestWrapper nativeRequest = (ContentCachingRequestWrapper) ((ServletWebRequest) request).getNativeRequest();
    String requestEntityAsString = new String(nativeRequest.getContentAsByteArray());

    log.debug(requestEntityAsString);

    return super.handleMethodArgumentNotValid(ex, headers, status, request);
  }
}

RequestBody and ResponseBody can be read-only once so other ways to achieve the body in the exception handler Visit here RequestBody 和 ResponseBody 可以只读一次所以其他方式在异常处理器中实现body访问这里

暂无
暂无

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

相关问题 使用spring数据jpa的spring boot。 如何从请求主体执行实体的部分更新? - Spring boot with spring data jpa. How can i perform partial update of entity from request body? 如何在springboot(REST)的ExceptionHandler类中获取请求体数据 - How to get the request body data inside ExceptionHandler class in springboot(REST) 我可以在 Spring Boot 中使用请求正文执行 GET 吗? - Can I do a GET with request body in Spring Boot? 如果http请求的内容类型为urlencoded,如何让我的spring启动控制器读取我对某个对象的http请求的主体? - How can I get my spring boot controller to read the body of my http request to an object if the http request has content type urlencoded? 我们如何在 java spring 引导中为不同的控制器获得不同的@ExceptionHandler? - How we can get different @ExceptionHandler for different controllers in java spring boot? 使用 Spring MVC 从 ExceptionHandler 获取请求值 - Get request values from ExceptionHandler using Spring MVC 如何检测 Spring Boot 中请求正文中的 JSON 对象是否为空? - How can I detect if the JSON object within Request body is empty in Spring Boot? 如何从请求体中得到一个Json插入到一个spring引导关系列中? - How to get a Json from the request body and insert it in a spring boot relationship column? Spring boot @ExceptionHandler 未达到 - Spring boot @ExceptionHandler is not reached 我们如何在 Spring 引导过滤器中获取和设置响应主体 - How can we get and set response body in Spring Boot Filter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM