title: Custom annotations to set HTTP response headers in a JAX-RS service
body: <p>I have a JAX-RS web service for which I would like to disable the same-origin policy via the new <a href="http://www.w3.org/TR/cors/" rel="nofollow">CORS</a> HTTP headers. (I am fully aware of the security implications.)</p>
  
  <p>I'd like to have a custom annotation that lets me set HTTP response headers. For example,</p>
  
  <pre><code>@ResponseHeaders({"Access-Control-Allow-Origin: *",
                    "Access-Control-Allow-Methods: GET"})
  // Or, alternatively:
  @AllowOrigins({"*"})
  public String resourceMethod() { ... }
  </code></pre>
  
  <p>This approach minimizes boilerplate code, but I'm not sure if there's a subtle technical limitation; JAX-RS provides many annotations to handle the HTTP request but not the response, with <code>@Produces</code> seeming to be the sole exception.</p>
  
  <p>I also prefer to stay away from too much web.xml configuration, if possible. Without explicitly needing to use a <a href="https://jsr311.dev.java.net/nonav/releases/1.1/javax/ws/rs/core/Response.ResponseBuilder.html" rel="nofollow">ResponseBuilder</a> (it's OK if an annotation uses one), is there a clean way to set custom HTTP response headers?</p>
  
  <p>To clarify, I'm looking for <em>annotations</em> that integrate with the various ways of setting HTTP response headers in order to minimize boilerplate code.</p>