一:示例web
@Aspect @Component public class WebLogAspect { private static Logger log = LoggerFactory.getLogger(WebLogAspect.class); private final ObjectMapper mapper; @Autowired public WebLogAspect(ObjectMapper mapper) { this.mapper = mapper; } @Pointcut("@annotation(org.springframework.web.bind.annotation.RequestMapping)") public void requestLog() { } @Pointcut("@annotation(org.springframework.web.bind.annotation.PostMapping)") public void postLog() { } @Before("requestLog() || postLog()") public void doBefore(JoinPoint joinPoint) { for (Object object : joinPoint.getArgs()) { if (object instanceof MultipartFile || object instanceof HttpServletRequest || object instanceof HttpServletResponse) { continue; } if (log.isInfoEnabled()) { log.info(joinPoint.getTarget().getClass().getName() + "." + joinPoint.getSignature().getName() + " : request parameter : " + object.toString()); } } } @AfterReturning(returning = "response", pointcut = "requestLog() || postLog()") public void doAfterReturning(Object response) throws Throwable { if (response != null) { log.info("response parameter : " + mapper.writeValueAsString(response)); } } }
二:說明spring
(1)請求參數對象最好重寫toString()方法app
(2)響應對象實現Serializable接口post
(3)示例僅供參數,能夠根據需求進行優化。優化