相當長的時間過去了,但其他人可能從中受益:
您可以定義一個@Around方面和攔截傳入的請求和它們各自的機構如下:
@Aspect
@Component
public class RequestResponseLoggingAdvice {
private static final Logger logger = LoggerFactory.getLogger(RequestResponseLoggingAdvice.class);
@Pointcut("within(@org.springframework.web.bind.annotation.RestController*)")
public void restcontroller() {}
@Pointcut("@annotation(org.springframework.web.bind.annotation.PostMapping)")
public void postmapping() {}
@Around("restcontroller() && postmapping() && args(.., @RequestBody body, request)")
public Object logPostMethods(ProceedingJoinPoint joinPoint, Object body, HttpServletRequest request) throws Throwable {
logger.debug(request.toString()); // You may log request parameters here.
logger.debug(body.toString()); // You may do some reflection here
Object result;
try {
result = joinPoint.proceed();
logger.debug(result.toString());
} catch(Throwable t) {}
}
}
請注意您的REST控制器方法必須具有上述方面的適當簽名才能登錄。示例一可能如下:
@PostMapping
public SampleDTO saveSample(@RequestBody Sample sample, HttpServletRequest request) {
//.....
}