Allow cross domain request to jersey web services

点点圈 提交于 2019-12-12 03:28:21

问题


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)


回答1:


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();


来源:https://stackoverflow.com/questions/32786766/allow-cross-domain-request-to-jersey-web-services

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!