相当长的时间过去了,但其他人可能从中受益:
您可以定义一个@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) {
//.....
}