Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP status code always returning 200 from HttpServletResponse

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.

like image 449
mrza Nomaan Beg Avatar asked Mar 11 '26 00:03

mrza Nomaan Beg


1 Answers

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.

like image 135
Karol Dowbecki Avatar answered Mar 12 '26 14:03

Karol Dowbecki



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!