参考文章,大谢
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();// urlSystem.err.println(request.getRequestURL());// methodSystem.err.println(request.getMethod());// ipSystem.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));}
}