Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set response header in Spring Boot

How can I set the response header for each call in my application made with Spring Boot? I would like to try to use a filter to intercept all the calls and be able to set the response header. I followed the guide Disable browser caching HTML5, but only set the request header, and not always.

like image 443
Giampiero Poggi Avatar asked Mar 22 '18 14:03

Giampiero Poggi


People also ask

How do you set a response header?

Select the web site where you want to add the custom HTTP response header. In the web site pane, double-click HTTP Response Headers in the IIS section. In the actions pane, select Add. In the Name box, type the custom HTTP header name.

Can we set response header?

You can set response headers, you can add response headers. And you can wonder what the difference is. But think about it for a second, then do this exercise. Draw a line from the HttpResponse method to the method's behavior.


1 Answers

There are three ways to do this:

  1. Set the response for a specific controller, in the Controller class:

    @Controller
    @RequestMapping(value = DEFAULT_ADMIN_URL + "/xxx/")
    public class XxxController
    ....
        @ModelAttribute
        public void setResponseHeader(HttpServletResponse response) {
           response.setHeader("Cache-Control", "no-cache");
           ....
        }
    

    or

    @RequestMapping(value = "/find/employer/{employerId}", method = RequestMethod.GET)
    public List getEmployees(@PathVariable("employerId") Long employerId, final HttpServletResponse response) {
        response.setHeader("Cache-Control", "no-cache");
        return employeeService.findEmployeesForEmployer(employerId);
    }
    
    1. Or you can put the response header for each call in the application (this is for Spring annotation-based, otherwise see automatically add header to every response):
    @Component
    public class Filter extends OncePerRequestFilter {
    ....
     @Override
     protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
            throws ServletException, IOException {
        //response.addHeader("Access-Control-Allow-Origin", "*");
        //response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); // HTTP 1.1.
        response.setHeader("Cache-Control", "no-store"); // HTTP 1.1.
        response.setHeader("Pragma", "no-cache"); // HTTP 1.0.
        response.setHeader("Expires", "0"); // Proxies.
        filterChain.doFilter(request, response);
     }
    }
    
    1. The last way I found is using an Interceptor that extends HandlerInterceptorAdapter; for more info see https://www.concretepage.com/spring/spring-mvc/spring-handlerinterceptor-annotation-example-webmvcconfigureradapter

      • create your Interceptor that extends HandlerInterceptorAdapter:
    public class HeaderInterceptor extends HandlerInterceptorAdapter {
    
      @Override
      public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object handler) {
        httpServletResponse.setHeader("Cache-Control", "no-store"); // HTTP 1.1.
        httpServletResponse.setHeader("Pragma", "no-cache"); // HTTP 1.0.
        httpServletResponse.setHeader("Expires", "0"); // Proxies.
        return true;
      }
    }
    
    • In your MvcConfig thath extends WebMvcConfigurerAdapter you must Override the addInterceptors method and add new Interceptor:
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        ....
        registry.addInterceptor(new HeaderInterceptor());
    }
    

I hope I was helpful!

like image 99
Giampiero Poggi Avatar answered Sep 17 '22 13:09

Giampiero Poggi