I'm using Spring AOP Aspect class to log request and response.
Below is my code
@AfterReturning(pointcut = "execution(* com.tr.dco.fp.ipam.controller..*.*(..))", returning = "retVal")
public void logAfter(JoinPoint joinPoint, Object retVal) {
HttpServletResponse response = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
}
Here, on successful response, I am always getting 200 status code even though the status code is 201 or any other.
Adding plain AspectJ aspect afer controller method is a bad idea. There are other pieces of Spring Web framework that can change the web response e.g. result code might be set with @ResponseStatus annotation.
You have to become part of the Spring Web processing logic e.g. by registering a HandlerInterceptorAdapter. Take a look at Spring - Modify response headers after controller processing to see how to do it.
Alternatively you can write a JavaEE filter before Spring's DispatcherServlet.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With