【发布时间】:2020-05-23 00:25:23
【问题描述】:
如何从调用我的 Springboot 应用程序的应用程序中获取当前请求的标头和正文?我需要提取这些信息。不幸的是,这不起作用。我尝试使用此代码示例 (https://stackoverflow.com/a/26323545/5762515) 获取当前请求:
public static HttpServletRequest getCurrentHttpRequest(){
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
if (requestAttributes instanceof ServletRequestAttributes) {
HttpServletRequest request = ((ServletRequestAttributes)requestAttributes).getRequest();
return request;
}
throw new IllegalArgumentException("Request must not be null!");
}
然后我试图得到尸体
ContentCachingRequestWrapper requestWrapper = (ContentCachingRequestWrapper) currentRequest;
String requestBody = new String(requestWrapper.getContentAsByteArray());
谁能告诉我我做错了什么? 提前致谢
【问题讨论】: