需求:为系统中所有的提交,修改,删除等等操作做日志记录,记录的内容包括:请求参数,返回参数,如果报错就存储报错信息。日志要添加一个日志类型。
因为有用到工具类,先放工具类吧
import java.util.List;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
public class JsonUtil {
// 定义jackson对象
private static final ObjectMapper mapper = new ObjectMapper();
/**
* 将对象转换成json字符串
* @param data
* @return
*/
public static String toJSONString(Object data) {
try {
String string = mapper.writeValueAsString(data);
return string;
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return null;
}
/**
* 将json结果集转化为对象
* @param jsonData
* @param beanType
* @return
*/
public static <T> T parseObject(String jsonData, Class<T> beanType) {
try {
T t = mapper.readValue(jsonData, beanType);
return t;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 将json数据转换成list
* @param jsonData
* @param beanType
* @return
*/
public static <T> List<T> parseArray(String jsonData, Class<T> beanType) {
JavaType javaType = mapper.getTypeFactory().constructParametricType(List.class, beanType);
try {
List<T> list = mapper.readValue(jsonData, javaType);
return list;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
然后开始正题
因为我的接口都是通过controller , 里面已经做过了@RequestBody,所以拿出参的时候试了好多方法
知道最后看到了一个大佬发的才醍醐灌顶,伪代码如下
/*@AfterReturning注解用于获取方法的返回值*/
@AfterReturning(pointcut = "print()", returning = "object")
public void getAfterReturn(Object object) {
endTime = System.currentTimeMillis() - startTime;
//返回参数
String outParam = JsonUtil.toJSONString(object);//这里用到了JsonUtil 工具类
System.out.println(outParam);//直接获取出参了
}
整个代码就在下面了
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;
/**
* @ClassName HttpRequestAspect
* @Description 项目
* @uthor zhangxubin
* @Date 2019/6/25 18:19
* @Version 1.0
*/
@Aspect
@Component
public class HttpRequestAspect {
@Autowired
private SystemTbApilogMapper systemTbApilogMapper;
public static long startTime;
public static long endTime;
/*@PointCut注解表示表示横切点,哪些方法需要被横切*/
/*切点表达式*/
@Pointcut("execution(public * com.reijing.his.rest.modular.controller.*.*(..))")
/*切点签名*/
public void print() {
}
/*@Before注解表示在具体的方法之前执行*/
@Before("print()")
public void before(JoinPoint joinPoint){
// log.info("前置切面before……");
startTime = System.currentTimeMillis();
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = requestAttributes.getRequest();
//入参
String inParam = JsonUtil.toJSONString(joinPoint.getArgs());//入参
String basePath1 = request.getServerName() + ":" + request.getServerPort() ;//IP地址 , 可以自己打印看一下
String requestURI = (request).getRequestURI();
String setRequestUrl = request.getScheme() + "://" +basePath1+requestURI;//十分完整的 Ip地址+方法路径 controller全路径
String results=request.getHeader("LoginInfo"); // getHead 头信息
String contextPath = request.getContextPath();//被请求的控制器
}
/*@After注解表示在方法执行之后执行*/
@After("print()")
public void after() throws IOException {
}
/*@AfterReturning注解用于获取方法的返回值*/
@AfterReturning(pointcut = "print()", returning = "object")
public void getAfterReturn(Object object) {
endTime = System.currentTimeMillis() - startTime;
//返回参数
String outParam = JsonUtil.toJSONString(object);
}
}