[英]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 中多次阅读 HttpServletRequest , Java org.springframework.Z2567A5EC9705EB7rapper8C984033EZW06 的代码示例
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.