✨这里是小松猿的博客✨小松,欢迎您的到来~✨
🍅系列专栏:无🍅
✈️本篇内容: spring拦截器获取请求体和响应体的内容
🍱本篇收录完整代码地址:无🍱
楔子
一文带你了解如何使用spring拦截器获取请求体和响应体的内容
springboot 日志记录接口的请求参数和响应结果的两种方式-拦截器和切面(具体代码)
前言:在生产中如果出现问题,我们想要查看日志,某个时间段用户调用接口的请求参数和响应的返回结果,通过日志来推测下用户当时做了什么操作。日志记录接口的请求参数和响应结果有利于我们排查生产的问题,但是也会给系统带来内存性能的问题。所以我们需要权衡其中的利弊来选择,下面就是记录日志两种方式的具体代码。
一、使用拦截器
本来以为这种方式只要自定义拦截器继承HandlerInterceptorAdapter类重写preHandle() 和 afterCompletion()就可以了。没想到还是遇到挺多坑。
请求参数:request.getParameterMap()可以获取到请求参数,但是如果接口是使用@RequestBody,就会发现得不到值。
响应结果:这是比较蛋疼一点,我几乎查了response的所有方法,都发现没法得到接口响应结果。
实现ResponseBodyAdvice类获取响应体的内容,需要在项目中引入jackson
maven坐标:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.3</version>
</dependency>
代码如下,result就是对应接口响应的结果转换成了json格式存放在了result字符串中,然后将result存放在了httpSession中,用于拦截器处进行获取:
@ControllerAdvice
public class InterceptResponse implements ResponseBodyAdvice<Object> {
private static final Integer MAX_LENGTH = 1000;
@Override
public boolean supports(MethodParameter methodParameter, Class<? extends HttpMessageConverter<?>> aClass) {
return true;
}
@Override
public Object beforeBodyWrite(Object body, MethodParameter methodParameter, MediaType mediaType, Class<? extends HttpMessageConverter<?>> aClass, ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {
String result;
ObjectMapper objectMapper = new ObjectMapper();
try {
result = objectMapper.writeValueAsString(body);
} catch (JsonProcessingException e) {
throw new RuntimeException("JSON转换失败", e);
}
Integer length = result.length();
result = result.substring(0, length);
HttpServletRequest httpServletRequest = ((ServletRequestAttributes)
RequestContextHolder.getRequestAttributes()).getRequest();
HttpSession httpSession = httpServletRequest.getSession(true);
//放到缓存里,以便于可以在HandlerInterceptor拦截里取出并打印出返回结果
httpSession.setAttribute("body", result)
return body;
}
}
定义拦截器继承HandlerInterceptorAdapter
@Component
@Slf4j
public class LogMonitorInterceptor extends HandlerInterceptorAdapter {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String requestParams = StringUtils.EMPTY;
if (request instanceof RequestWrapper) {
requestParams = ((RequestWrapper) request).getBody();
}
if(StrUtil.isEmpty(requestParams)){
requestParams = JSON.toJSONString(request.getParameterMap());
}
log.info("request: uri:{} , type:{} , ip:{}, operatorId:{}, operatorName:{}, params:{}",
request.getRequestURI(),request.getMethod(),request.getRemoteAddr(),
WebUtils.getId(),WebUtils.getUsername(), requestParams );
return true;
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
HttpSession httpSession = request.getSession();
String result = (String) httpSession.getAttribute("body");
log.info("response: url:{} , type:{} , ip:{}, operatorId:{}, operatorName:{}, result:{}",
request.getRequestURI(),request.getMethod(),request.getRemoteAddr(),
WebUtils.getId(),WebUtils.getUsername(), result );
}
}
将拦截器注册在spring中:
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Autowired
private LogMonitorInterceptor logMonitorInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
// 接口操作日志拦截器
registry.addInterceptor(logMonitorInterceptor);
}
}