以前用过手撕 HttpRequest 获取 Body、Params、Header 等 API 的时候,那叫一个复杂,自从用了 Hutool 神器后,发现如此的轻巧
java
// 获取请求头
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
Map<String, String> requestHeaderMap = ServletUtil.getHeaderMap(request);
// 获取请求体
Map<String, Object> requestParamMap = new LinkedHashMap<>();
String body = ServletUtil.getBody(request);
Map<String, String> params = ServletUtil.getParamMap(request);