问题
I know this question is already posted here , but I am stuck at one point and I am not getting exact solution so posting it again here.
I have written a simple RESTFul web service with Jersey . In a POST method , I am passing JSON object in request.
I am using annotated @XmlRootElement Class, and the same is used for POST method.
The code looks like as follows - My POJO class
@XmlRootElement
public class WelcomeForm {
public String title;
public WelcomeForm(){
title = "";
}
public WelcomeForm(String inTitle){
title = inTitle;
}
}
Web service is as follows
@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;
}
}
When I tried to run a simple html code by passing a json object in request am getting an error as
/* 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
Then I added Custom Filer Class, to resolve Access-Control-Origin not allowed error
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; }}
But its of no useful , I got same error again.
Here is my html code
<!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>
So what is wrong in this ? Am I missing something ? And when we do get Access-Control-Allow-Origin error ? And I have done changes in web.xml file to mention the filter class
<init-param>
<param-name>com.sun.jersey.spi.container.ContainerResponseFilters</param-name>
<param-value>com.ws.form.CustomResponseFilters</param-value>
</init-param>
Am I missing Anything else ??
回答1:
The error (Its not actually an error) com.sun.jersey.server.wadl.generators.AbstractWadlGeneratorGrammarGenerator attachTypes INFO: Couldn't find grammar element for class java.lang.String
is unrelated to the cross domain problem.
I'm not sure why its happening I have read it is something to do with using a default constructor along with @XmlRootElement. Anyways 2 options to get rid of it either put
@OPTIONS
public Response myResource() {
return Response.ok().build();
}
or disable wadl in the web.xml
<init-param>
<param-name>com.sun.jersey.config.feature.DisableWADL</param-name>
<param-value>true</param-value>
</init-param>
来源:https://stackoverflow.com/questions/16247110/a-json-object-in-a-post-request-for-webserver-with-jersey