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