Search code examples
web-serviceshttp-headersjax-rsresponseresteasy

Injecting a custom response header in RESTEasy JAX-RS


I have RESTEasy (JAX-RS) server with about 60 services (so far). I would like to automatically inject a custom response header to provider callers with the server build time: X-BuildTime: 20100335.1130.

Is there an easy way to do this without modifying each of my services?

I am trying to use a class that implements org.jboss.resteasy.spi.interception.PostProcessInterceptor with annotations @Provider and @ServerInterceptor, but I can't figure out how to modify the ServerResponse that is passed into my postProcess() method.


Solution

  • I think using javax.servlet.Filter will be a much easier solution:

    public void doFilter ( ServletRequest request, ServletResponse response, FilterChain chain ) throws IOException, ServletException {
       HttpServletResponse httpResponse = (HttpServletResponse)response;
       httpResponse.setHeadder(header, headerValue);
       chain.doFilter(request, response);
    }
    

    configure it in web.xml for the relevant urls, and you are done.