我知道这个问题已经发布在这里了,但是我被困在某一点上,我没有得到确切的解决方案,所以在这里再次发布。
我在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
然后我添加了Custom Filer Class,以解决Access-Control-Origin不允许错误
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>
我错过了其他什么吗?
答案 0 :(得分:0)
错误(实际上并非错误) com.sun.jersey.server.wadl.generators.AbstractWadlGeneratorGrammarGenerator attachTypes 信息:无法找到类java.lang.String
的语法元素与跨域问题无关。
我不确定为什么它发生了我已经读过它与使用默认构造函数和@XmlRootElement有关。无论如何要摆脱它的2个选项
@OPTIONS
public Response myResource() {
return Response.ok().build();
}
或禁用web.xml中的wadl
<init-param>
<param-name>com.sun.jersey.config.feature.DisableWADL</param-name>
<param-value>true</param-value>
</init-param>