CORS support for Jax-rs 2 AsyncResponse?

223 views Asked by At

I created a rest webservice using Jax-rs 2. It uses the AsyncResponse to return the response. How I can add CORS support to my rest web services ? So any one can access it with out any cross domain issues ?

My sample rest webservice is as follows

@GET
@Path("/test")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)

public void test(@Suspended AsyncResponse asyncResponse){
        asyncResponse.resume(Response.ok().build());
}

Thank you!

1

There are 1 answers

0
Paul Samsotha On

RestEasy has the CorsFilter. But since you are using Glassfish, you are most likely using Jersey. Jersey doesn't seem to have any such class we can use. That said, the CorsFilter for RestEasy implements ContainterRequestFilter and ContainerResponseFilter (Note, this is a Jersey 2.x (JAX-RS 2.0) feature).

You can also see the source code for the CorsFilter here at GrepCode. You'll basically want to implements something similar to that class, then register it is as a singleton with the application.