参考文章,大谢
https://www.imooc.com/video/14346/0
编写HttpAspect类
import javax.servlet.http.HttpServletRequest;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import com.alibaba.fastjson.JSON;
@Aspect
@Component
public class HttpAspect {
@Pointcut("execution(public * com.**.controller..*.*(..))")
public void log() {
}
@AfterReturning(returning = "object", pointcut = "log()")
public void doAfterReturning(JoinPoint joinPoint, Object object) {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
// url
System.err.println(request.getRequestURL());
// method
System.err.println(request.getMethod());
// ip
System.err.println(request.getRemoteAddr());
// 类+方法
System.err.println(joinPoint.getSignature().getDeclaringTypeName() + joinPoint.getSignature().getName());
// 请求参数
System.err.println(JSON.toJSONString(joinPoint.getArgs()));
// 返回值
System.err.println(JSON.toJSONString(object));
}
}