title: Set JAX-RS response headers in implementation without exposing HttpServletResponse in interface
body: <p>I have a RESTful server implementation as well as a library for clients to make the calls, all using JAX-RS. The server components are divided up into interface <code>FooResource</code> and implementation <code>FooResourceService</code>.</p>
  
  <p>In order for the client and server libraries to share RESTful path and other definitions, I wanted to split out the <code>FooResource</code> interface into its own project:</p>
  
  <pre><code>@Path(value = "foo")
  public interface FooResource {
  
    @GET
    public Bar getBar(@PathParam(value = "{id}") int id) {
  </code></pre>
  
  <p>I want to set some headers in the response. One easy way to do this is to use <code>@Context HttpServletResponse</code> in the method signature:</p>
  
  <pre><code>  public Bar getBar(@PathParam(value = "{id}") int id, @Context HttpServletResponse servletResponse) {
  </code></pre>
  
  <p>But the problem is that this exposes implementation details in the interface. More specifically, it suddenly requires my REST definition project (which is shared between the client and server library) to pull in the <code>javax.servlet-api</code> dependency---something the client has no need up (or desire for).</p>
  
  <p><strong>How can my RESTful resource service implementation set HTTP response headers without pulling in that dependency in the resource interface?</strong></p>
  
  <p>I saw <a href="https://stackoverflow.com/a/4625289/421049">one post</a> recommending I inject the HttpServletResponse as a class member. But how would this work if my resource service implementation is a singleton? Does it use some sort of proxy with thread locals or something that figures out the correct servlet response even though the singleton class is used simultaneously by multiple threads? Are there any other solutions?</p>