You can easily do those using Handler Interceptors which allow you to modify the request processing lifecycle within Spring MVC. Interceptors are a very powerful tool that allows us to add functionality to the request processing lifecycle at 3 different points:
- before a controller handles a request
- after a controller method completed its code execution
- when the view is about to be rendered and sent back as the response to the client
I think option 2 will suit your needs.
Then you can write something like this:
public class MyInterceptor extends HandlerInterceptorAdapter {
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
super.postHandle(request, response, handler, modelAndView);
//add your two headers on the response here
}
}
step 2 is to register that interceptor in your configuration file, add the next lines to your XML configuration:
<mvc:interceptors>
<bean Class="your interceptor class">
</mvc:interceptors>
from now on that interceptor will apply for every request.