使用JERSEY的Web服务器的POST请求中的JSON对象 [英] A JSON object in a POST request for webserver with JERSEY
问题描述
我知道这个问题已经在这里发布了,但是我一度陷入困境,我没有确切的解决方案,因此请在此处再次发布.
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.
我已经用Jersey编写了一个简单的RESTFul Web服务. 在POST方法中,我正在请求中传递JSON对象.
I have written a simple RESTFul web service with Jersey . In a POST method , I am passing JSON object in request.
我正在使用带注释的@XmlRootElement类,并且用于POST方法也是如此.
I am using annotated @XmlRootElement Class, and the same is used for POST method.
代码如下所示 -我的POJO班
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服务如下
@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代码时,出现错误
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
-
然后我添加了自定义文件管理器类,以解决Access-Control-Origin不允许的错误
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.
这是我的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文件进行了更改,以提及过滤器类
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>
我还想念其他东西吗?
推荐答案
错误(实际上不是错误) com.sun.jersey.server.wadl.generators.AbstractWadlGeneratorGrammarGenerator attachTypes 信息:找不到类java.lang.String的语法元素
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
与跨域问题无关.
我不确定为什么我已经读过它,这与将默认构造函数与@XmlRootElement一起使用有关.无论如何,要摆脱它的2种选择
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(); }
或在web.xml中禁用wadl
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>
这篇关于使用JERSEY的Web服务器的POST请求中的JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!