Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HandlerInterceptor.afterCompletion() in spring MVC changes response code

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] enter image description here

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.

like image 736
subhashlg26 Avatar asked May 05 '15 09:05

subhashlg26


People also ask

Which of the following methods are valid HandlerInterceptor methods?

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.

How do you intercept a response in spring boot?

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.

What is HandlerInterceptor?

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.

Why do we need interceptors in spring?

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.


1 Answers

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

like image 166
Master Slave Avatar answered Oct 12 '22 00:10

Master Slave