基于Spring框架的记录操作日志

HTTP 接口调用操作日志

对于一些HTTP API接口,有时候需要记录日志,了解什么用户在什么时间调用了什么接口,调用的参数是什么,返回的结果是什么。但是,又不能侵入业务代码逻辑。

http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/servlet/mvc/method/annotation/ResponseBodyAdvice.html

在什么地方记录?

首先想到的时候使用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
    * &#
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值