RESTful webservice:如何在 java 中设置标头以接受 Access-Control-Allow-Origin 允许的 XMLHttpRequest
我有一个 RESTful 网络服务,它将返回字符串,它是用 Java (JAX-WS) 编写的.我的问题是当我使用以下 URL 向该 Web 服务发送请求时:
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 无法加载 url Origin localhost is not allowed通过访问控制允许来源
XMLHttpRequest cannot load url Origin localhost is not allowed by Access-Control-Allow-Origin
我该如何处理这个问题?
How can I handle this issue?
Java 代码:
@GET
@Path("/getsample")
public Response getMsg() {
String output = "Jersey say : " ;
return Response.status(200).entity(output).build();
}
推荐答案
阅读这里关于你的 CORS 问题:http://enable-cors.org/
Read here about your issue CORS : http://enable-cors.org/
检查这是否对您的 getMsg() 方法有帮助:return Response.ok(output).header("Access-Control-Allow-Origin", "*").build();
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;
}
}
稍后注册 win web.xml:
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")
("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 "*"
.
相关文章