简体   繁体   English

如何在 spring 过滤器中获取请求正文参数?

[英]How to get request body params in spring filter?

I want to log the request params in the http request via Spring filter or aspect.我想通过 Spring 过滤器或方面在 http 请求中记录请求参数。 I tried different ways but either request params are null or method is not called.我尝试了不同的方法,但请求参数是 null 或方法未被调用。 I am using POSTMAN and it's a POST request我正在使用 POSTMAN 这是一个 POST 请求

http://localhost:8080/AvailableData http://localhost:8080/AvailableData

sample request body:样品请求正文:

{"keyUserAgent":"CFNetwork/1209 Darwin/20.2.0","locale":"en_US","eid":"8904977033","sessionId":"VGA-G20201030-776878787-1AD5-11EB-895C-H78789GJJH"} {"keyUserAgent":"CFNetwork/1209 Darwin/20.2.0","locale":"en_US","eid":"8904977033","sessionId":"VGA-G20201030-776878787-1AD5-11EB-895C-H78789GJJH "}

method 1: here "@Override" method of beforeRequest() is called but overloaded method that I created is not called(I added @RequestBody to get the body as per other solutions).方法1:这里调用了 beforeRequest() 的“@Override”方法,但没有调用我创建的重载方法(我添加了@RequestBody 以根据其他解决方案获取正文)。

@Component
public class CustomLoggingFilter extends AbstractRequestLoggingFilter {
      
            protected void beforeRequest(HttpServletRequest request, String message,@RequestBody RequestDTO requestBody) {
            requestBody.getKeyUserAgent();
            requestBody.getEid();
            System.out.println("Eid: "+requestBody.getEid());
            System.out.println("getKeyUserAgent: "+requestBody.getKeyUserAgent());
            
        }  
        
    }

method 2: here it is coming as null方法 2:这里是 null

@Aspect
@Component
@Order(1)
public class LogAspect {

    private final static Logger logger = LoggerFactory.getLogger(LoggerAspect.class);
      @Around("allControllerMethods() && args(..,@annotation(org.springframework...RequestBody) requestBody) ")
      public Object controllerEvents(ProceedingJoinPoint jp, Object requestBody) throws Throwable {
            
          ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
             HttpServletRequest request = attributes.getRequest();
             MethodSignature signature = (MethodSignature) jp.getSignature();
              Method method = signature.getMethod();
              Object resObject = jp.proceed();
              Object sessionId = attributes.getSessionId()
              if (requestBody != null) {
                  String keyUserAgent = request.getParameter("keyUserAgent");
                  System.out.println("keyUserAgent : " + keyUserAgent);
                  
                }
              
          return resObject;
      } 

First of all, there is a problem with reading data from HttpRequest for logging and later for processing as class HttpServletRequest only allows to read its contents once, and any repeating attempt to read it will cause an Exception.首先,从 HttpRequest 读取数据以进行日志记录和稍后进行处理时存在问题,因为 class HttpServletRequest只允许读取其内容一次,任何重复尝试读取它都会导致异常。 So, spring boot provides a solution for that with usage of class ContentCachingRequestWrapper .因此, spring 启动为使用 class ContentCachingRequestWrapper提供了解决方案。 The idea is that you read the entire context of your request once in the filter and copy the contents into your wrapper class that allows multiple reads.这个想法是您在过滤器中读取请求的整个上下文并将内容复制到允许多次读取的包装器 class 中。 After that, you continue the chain with your wrapper class (that indirectly implements HttpServletRequest).之后,您使用包装器 class(间接实现 HttpServletRequest)继续该链。 So now in one of your filters (that must be configured after the filter that replaces HttpServletRequest with ContentCachingRequestWrapper you can read and log your request parameters and later on you still can read your request for handling it. I implemented this in our project and it works like a charm. Here are few links to the articles explaining how to do it. Reading HttpServletRequest Multiple Times in Spring , Java Code Examples for org.springframework.web.util.ContentCachingRequestWrapper因此,现在在您的一个过滤器中(必须在将 HttpServletRequest 替换为 ContentCachingRequestWrapper 的过滤器之后进行配置,您可以读取并记录您的请求参数,稍后您仍然可以读取您的处理请求。我在我们的项目中实现了它并且它有效就像一个魅力。这里有一些文章的链接解释了如何做到这一点。 在 Spring 中多次阅读 HttpServletRequestJava org.springframework.Z2567A5EC9705EB7rapper8C984033EZW06 的代码示例

暂无
暂无

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

相关问题 Spring Boot-如何在Spring RestController中的映射中获取所有请求参数? - Spring Boot - How to get all request params in a map in Spring RestController? Spring MVC - 如何在 Spring 控制器的地图中获取所有请求参数? - Spring MVC - How to get all request params in a map in Spring controller? 如何在对 Spring MVC 控制器的 GET 请求中接受日期参数? - How to accept Date params in a GET request to Spring MVC Controller? 我如何在春季从curl -X请求获取参数? - How can i get params in spring from curl -X request? 如何使用 Spring 中的 restTemplate 使用请求正文发出 GET 请求 - How to make a GET request with request body using restTemplate in Spring 带Jersey的Spring启动 - 过滤请求主体属性 - Spring boot with Jersey - Filter request body properties 如何从 Spring Boot 过滤器中的 servletResponse 获取响应正文 - How to get Response Body from servletResponse in Spring Boot Filter 我们如何在 Spring 引导过滤器中获取和设置响应主体 - How can we get and set response body in Spring Boot Filter Spring MVC:如何在请求浏览器中设置主体参数,以及如何在Spring MVC的控制器中获取该主体参数? - Spring MVC: How to set body parameter in request browser and how to get this body parameters in controller in Spring MVC? 如何在Spring Restful Web服务内的请求正文中发送字符串? - How to get a string sent in body of a request inside a Spring restful webservice?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM