RESTful webservice:如何在java中设置头以接受AccessHttpRequest允许的Access-Control-Allow-Origin [英] RESTful webservice : how to set headers in java to accept XMLHttpRequest allowed by Access-Control-Allow-Origin
问题描述
我有一个RESTful webservice将返回字符串,它是用Java(JAX-WS)编写的。
我的问题是当我发送请求到那个网络服务与URL像:
I have a RESTful webservice which will return string and it was written in Java (JAX-WS). My problem is when I send request to that webservice with URL like :
http:// localhost:8080 / project / webservices / getlist / getListCustomers
在控制台中给我的错误信息如下:
In the console it's giving me the error message below:
XMLHttpRequest无法加载网址原始本地主机不允许
由Access-Control-Allow-Origin
XMLHttpRequest cannot load url Origin localhost is not allowed by Access-Control-Allow-Origin
如何处理此问题?
Java代码:
@GET
@Path("/getsample")
public Response getMsg() {
String output = "Jersey say : " ;
return Response.status(200).entity(output).build();
}
推荐答案
检查这个一个帮助你在你的getMsg()方法:
return Response.ok(output).header(Access-Control-Allow-Origin,* code>
Check if this one help you in your getMsg() method: return Response.ok(output).header("Access-Control-Allow-Origin", "*").build();
如果以上不工作,尝试添加Jersey过滤器到您的服务。创建过滤器类:
If above doesn't work try to add Jersey filter to your service. Create filter class:
package your.package;
public class CORSFilter implements ContainerResponseFilter {
@Override
public ContainerResponse filter(ContainerRequest creq, ContainerResponse cresp) {
cresp.getHttpHeaders().putSingle("Access-Control-Allow-Origin", "*");
cresp.getHttpHeaders().putSingle("Access-Control-Allow-Credentials", "true");
cresp.getHttpHeaders().putSingle("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT, OPTIONS, HEAD");
cresp.getHttpHeaders().putSingle("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With");
return cresp;
}
}
p>
And register later win web.xml with:
<servlet>
<servlet-name>CORS Filter</servlet-name>
<servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>com.sun.jersey.spi.container.ContainerResponseFilters</param-name>
<param-value>your.package.CORSFilter</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>CORS Filter</servlet-name>
<url-pattern>/webservices/*</url-pattern>
</servlet-mapping>
另一种解决方案是在资源中使用此代码来提供
OPTIONS
。把这放在你有@GET的类中。
Another solution is to use this code inside your resource to provide
OPTIONS
for the browser. Put this in the class where you have @GET.
@OPTIONS
@Path("/getsample")
public Response getOptions() {
return Response.ok()
.header("Access-Control-Allow-Origin", "*")
.header("Access-Control-Allow-Methods", "POST, GET, PUT, UPDATE, OPTIONS")
.header("Access-Control-Allow-Headers", "Content-Type, Accept, X-Requested-With").build();
}
如果没有这项工作,用于访问此资源的自定义域的Access-Control-Allow-Origin标头提供的
*
例如如果你从 http:// localhost :: 8080
调用这个(Access-Control-Allow-Origin,http: // localhost:8080)
而不是星号*
。
If non of this work, try to exchange the
"*"
provided for "Access-Control-Allow-Origin" header with your custom domain where you access this resource. I.g. If you call this from http://localhost::8080
use something like this ("Access-Control-Allow-Origin", "http://localhost:8080")
instead of asterisk "*"
.
这篇关于RESTful webservice:如何在java中设置头以接受AccessHttpRequest允许的Access-Control-Allow-Origin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!