我知道这个问题已经在这里发布了,但是我被困在了一个地方,我没有得到确切的解决方案,所以在这里再次发布。
我用 Jersey 编写了一个简单的 RESTFul Web 服务。在 POST 方法中,我在请求中传递 JSON 对象。
我正在使用带注释的 @XmlRootElement 类,同样用于 POST 方法。
代码如下所示 - 我的 POJO 类
@XmlRootElement
public class WelcomeForm {
public String title;
public WelcomeForm(){
title = "";
}
public WelcomeForm(String inTitle){
title = inTitle;
}
}
Web服务如下
@Path("/welcome")
public class WelcomeImpl {
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public WelcomeForm welcome(WelcomeForm welcomeFormObject) {
WelcomeForm form = new WelcomeForm();
form.title = " Connected ... ";
System.out.println("welcomeFormObject *** "+welcomeFormObject.title);
return form;
}
}
当我尝试通过在请求中传递 json 对象来运行简单的 html 代码时,出现错误
/* in browser I get error as follows */
XMLHttpRequest cannot load http://localhost:8081/ws/welcome. Origin http://localhost:8080 is not allowed by Access-Control-Allow-Origin.
/* in eclipse I get error as follows */
Apr 27, 2013 5:59:51 AM com.sun.jersey.server.wadl.generators.AbstractWadlGeneratorGrammarGenerator attachTypes
INFO: Couldn't find grammar element for class java.lang.String
Apr 27, 2013 5:59:51 AM com.sun.jersey.server.wadl.generators.AbstractWadlGeneratorGrammarGenerator attachTypes
INFO: Couldn't find grammar element for class java.lang.String
Apr 27, 2013 5:59:51 AM com.sun.jersey.server.wadl.generators.AbstractWadlGeneratorGrammarGenerator attachTypes
INFO: Couldn't find grammar element for class java.lang.String
Apr 27, 2013 5:59:51 AM com.sun.jersey.server.wadl.generators.AbstractWadlGeneratorGrammarGenerator attachTypes
INFO: Couldn't find grammar element for class java.lang.String
Apr 27, 2013 5:59:51 AM com.sun.jersey.server.wadl.generators.AbstractWadlGeneratorGrammarGenerator attachTypes
INFO: Couldn't find grammar element for class java.lang.String
Apr 27, 2013 5:59:51 AM com.sun.jersey.server.wadl.generators.AbstractWadlGeneratorGrammarGenerator attachTypes
INFO: Couldn't find grammar element for class java.lang.String
Apr 27, 2013 5:59:51 AM com.sun.jersey.server.wadl.generators.AbstractWadlGeneratorGrammarGenerator attachTypes
INFO: Couldn't find grammar element for class java.lang.String
然后我添加了自定义文件管理器类,以解决 Access-Control-Origin not allowed 错误
public class CustomResponseFilters implements ContainerResponseFilter { public ContainerResponse filter(ContainerRequest request, ContainerResponse response) { // After request processed response.getHttpHeaders().add("Access-Control-Allow-Origin", "http://MyIPAddress:8080"); response.getHttpHeaders().add("Access-Control-Allow-Methods", "PUT, GET, POST, DELETE, OPTIONS"); response.getHttpHeaders().add("Access-Control-Allow-Headers", "Content-Type"); return response; }
}
但它没有用,我又遇到了同样的错误。
这是我的html代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
function CallService() {
var obj = {};
obj.title ="Hello!!!";
console.log(obj.title);
$.ajax({
type: "POST",
url: "http://MyIPAddress:8081/ws/welcome",
data: JSON.stringify(obj),
contentType: "application/json; charset=utf-8",
dataType: "application/json",
success: function (response) {
console.log(" **** success ** "+response);
}
});
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type=button onclick="CallService()" name="Button1" value="Button1" />
</div>
</form>
</body>
</html>
那么这有什么问题呢?我错过了什么吗?当我们确实得到 Access-Control-Allow-Origin 错误时?我已经在 web.xml 文件中进行了更改以提及过滤器类
<init-param>
<param-name>com.sun.jersey.spi.container.ContainerResponseFilters</param-name>
<param-value>com.ws.form.CustomResponseFilters</param-value>
</init-param>
我还缺少什么吗??