springboot 监听请求

加个这个类就可以了

import org.springframework.stereotype.Component;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;

@Component
public class LoggingFilter implements Filter {

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
HttpServletRequest httpRequest = (HttpServletRequest) request;
System.out.println("Request URL: " + httpRequest.getRequestURL());

StringBuilder requestBody = new StringBuilder();
BufferedReader reader = httpRequest.getReader();
String line;
while ((line = reader.readLine()) != null) {
requestBody.append(line).append('\n');
}
System.out.println("Request Body: " + requestBody);

chain.doFilter(request, response);
}
}
posted on 2024-03-31 17:38  woxiangbo  阅读(29)  评论(0编辑  收藏  举报