I have an application based on MVC and I want to log inputs/outputs from all my controllers.
Also, each of my controller methods is annotated with some specific annotation (eg: @MyControllerMethod
).
I wrote an Annotated Aspect which works fine when I use the annotation (eg: LogRequestReply
) from the aspect on my controller methods.
@Around("@annotation(com.xyz.LogRequestReply)")
public Object logRequestResponse(ProceedingJoinPoint pjp) throws Throwable {
@MyControllerMethod
@LogRequestReply /* It invokes my logRequestResponse method */
public ResponseEntity controllerMethodA(...) {}
However, I have a lot of controller methods and each with annotation MyControllerMethod
.
Is there a way I can make my aspect annotation make work on all controller methods by default, such that each of the methods automatically calls my aspect logger and I can log the inputs/outputs?
Note: I also looked into writing an interceptor extending from HandlerInterceptorAdapter
. But it has its own complexity of being able to log request/response where the stream can only be read once.
Please suggest.
One option is to let the @Around definition less restrictive, add a restriction by package or something like that. Internally you can check if this is a Controller by this check bellow and then print the request/response:
@Pointcut("within(com.mypackage.controller..*)")
private void inControllerPackage() {
}
@Around("inControllerPackage()")
public Object logRequestResponse(ProceedingJoinPoint pjp) throws Throwable {
if (pjp.getSignature().getDeclaringType().isAnnotationPresent(RestController.class)) {
...
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.