I am using spring MVC framework. I want to log error statuses whenever exception is thrown, so afterCompletion
method is used in HanlderInterceptor
.
@Override
public void afterCompletion( final HttpServletRequest request, final HttpServletResponse response, final Object handler, final Exception ex)
{
final int responseCode = response.getStatus();
s_logger_error.error("status code: " + responseCode );
}
This code works fine if I run this as an application on local machine. But when we host it on the jetty server, UI gets correct error response (In my case 409
), but in this method it gets logged as 200
.
[Image from remote debug where it shows status=200
but in response it is 409
]
Can somebody help to figure out why there is change in the response code?
I am using sprint 1.1.7.RELEASE spring boot version and jetty-distribution-9.2.10.v20150310.
The HandlerInterceptor contains three main methods: prehandle() – called before the execution of the actual handler. postHandle() – called after the handler is executed. afterCompletion() – called after the complete request is finished and the view is generated.
To work with interceptor, you need to create @Component class that supports it and it should implement the HandlerInterceptor interface. preHandle() method − This is used to perform operations before sending the request to the controller. This method should return true to return the response to the client.
public interface HandlerInterceptor. Workflow interface that allows for customized handler execution chains. Applications can register any number of existing or custom interceptors for certain groups of handlers, to add common preprocessing behavior without needing to modify each handler implementation.
Spring Interceptor are used to intercept client requests and process them. Sometimes we want to intercept the HTTP Request and do some processing before handing it over to the controller handler methods. That's where Spring MVC Interceptor come handy.
You need to make sure that you call setStatus
method on your response
object on exception.
If you assert this, than upgrading to spring boot version 1.1.11 can fix your issue. It concerns this fix. Prior to the fix, the ErrorPageFilter
was masking the response status of wrapped response in the cases where sendError
method is not explicitely called on the ErrorWrapperResponse
.
The code after the fixed changed from simple return this.status
to
if (this.errorToSend) {
return this.status;
}
else {
// If there was no error we need to trust the wrapped response
return super.getStatus();
}
that is why I believe that the upgrade will fix your issue.
Finally, if you issue persist, debugging through ErrorPageFilter
should indicate the origin of the problem
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