Adding HTTP headers to the postHandle method with Spring interceptor

I have a Spring Interceptor that is trying to add an HTTP header to the postHandle () method.

public void postHandle(HttpServletRequest req, HttpServletResponse resp, 
             Object obj1, ModelAndView mv)
        throws Exception {
        response.setHeader("SomeHeaderSet", "set");
        response.addHeader("SomeHeaderAdd", "added");
    }
}

However, no headers are added using setHeader () or addHeader ().

Is it possible to do this in an interceptor? I decided that it would be, but it does not work.

Regards, Dustin

+3
source share
3 answers

Well, I figured it out ... Kinda ...

It turns out the same issue with Jetty and Tomcat (the MAYBE value was a container issue). So that...

Debugging to ensure that the response object contains the correct header value until Spring returns to the container. Result: the HttpServletResponse instance still had the correct header value.

, response.setContentLength() , - . , .

, , response.setContentLength() ? , - - .

+1

preHandle? , .

+1

, , web.xml( )

<filter>
  <filter-name>etagFilter</filter-name>
    <filter-class>org.springframework.web.filter.ShallowEtagHeaderFilter</filter-class>
</filter>

<filter-mapping>
  <filter-name>etagFilter</filter-name>
  <servlet-name>myServlet</servlet-name>
</filter-mapping>
0

Source: https://habr.com/ru/post/1698866/


All Articles