Jagdeep
Jagdeep

Reputation: 21

Allow cross domain request to jersey web services

Facing issue related to allowing cross domain requests in jersey. I tried to implement com.sun.jersey.spi.container.ContainerResponseFilters, but not able to configure it.

Getting exception, when try to send the request from browser.

org.apache.catalina.core.StandardWrapperValve invoke SEVERE: Servlet.service() for servlet [JAX-RS Servlet] in context with path [/crawlerweb] threw exception [Servlet execution threw an exception] with root cause java.lang.AbstractMethodError: javax.ws.rs.core.UriBuilder.uri(Ljava/lang/String;)Ljavax/ws/rs/core/UriBuilder; at javax.ws.rs.core.UriBuilder.fromUri(UriBuilder.java:119) at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.j‌​ava:662)

Upvotes: 0

Views: 496

Answers (1)

Mike Ezzati
Mike Ezzati

Reputation: 3166

You must implement support for CORS in your jersey service. For cross domain request, browser first sends a preflight httprequest with OPTIONS method. Then your server must return certain CORS headers in response.

Access-Control-Allow-Origin :*
Access-Control-Allow-Methods: GET, POST

Browsers checks for those headers if they are present then it makes the real httprequest that you want.

You can try this

return Response.ok() 
        .entity(podcastById, detailed ? new Annotation[]{PodcastDetailedView.Factory.get()} : new Annotation[0])
        .header("Access-Control-Allow-Origin", "*")
        .header("Access-Control-Allow-Methods", "GET, POST")
        .allow("OPTIONS").build();

Upvotes: 0

Related Questions