HTTP 接口调用操作日志
对于一些HTTP API接口,有时候需要记录日志,了解什么用户在什么时间调用了什么接口,调用的参数是什么,返回的结果是什么。但是,又不能侵入业务代码逻辑。
在什么地方记录?
首先想到的时候使用Filter,但是发现要获取response中的body内容,太麻烦,而ResponseBodyAdvice这个接口恰恰可以满足我们的需求。
/**
* 允许兹定于response内容,只能在@ResponseBody或者@responseEntity注解的controller方法
* 有用。改方法会在把body内容通过HttpMessaheConverter写入前调用
*
* 实现该接口并通过 @ControllerAdvice来注册
*
* @since 4.1
*/
public interface ResponseBodyAdvice<T> {
/**
* 当返回true的时候会调用beforeBodyWrite方法,否则不会
*/
boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType);
/**
* {@code HttpMessageConverter}选择后和写入前调用
* @param body the body to be written
* @param returnType the return type of the controller method
* @param selectedContentType the content type selected through content negotiation
* @param selectedConverterType the converter type selected to write to the response
* @param request the current request
* &#