Google App Engine - 如何将对象返回到我的 servlet?
有谁知道如何将对象(更具体地说是 List
)(数据库中查询的结果)发送到我的 servlet,这是另一个 Java 应用程序,不在 Google App Engine 中。
更新:我在 GAE 中的 servlet 工作正常,它序列化我的 List
结果:
public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
String titulo = req.getParameter("titulo");
String json = "";
PersistenceManager pm = PMF.get().getPersistenceManager();
Query query = pm.newQuery("select from "+Video.class.getName()+ " where titulo.startsWith('"+titulo+"')");
List<Video> video = (List<Video>) pm.newQuery(query).execute();
json = new Gson().toJson(video);
System.out.println("SERIALIZED >> " + json);
res.setContentType("application/json");
res.setCharacterEncoding("UTF-8");
res.getWriter().write(json);
}
我的调用 servlet 有此方法:
public void receberMetaDados(String titulo) throws IOException, Exception{
InputStream input = new URL("http://localhost:8888/serve?titulo="+titulo).openStream();
Reader reader = new InputStreamReader(input, "UTF-8");
List<Video> results = new Gson().fromJson(reader, new TypeToken<List<Video>>(){}.getType());
}
我收到以下异常:
com.google.gson.JsonParseException:JsonDeserializer com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter@2d7440 无法反序列化 json 对象 [{"id":"30102010113847691504","titulo":"天使城市"," descricao":"Um belo filme","date":"30/11/2010 13:38:47"},{"id":"30102010115514196289","titulo":"天使之城","descricao": "Um belo filme","date":"30/11/2010 13:55:14"},{"id":"3010201011561620697","titulo":"天使之城","descricao":"Um belo filme","date":"30/11/2010 13:56:01"},{"id":"3010201012829669834","titulo":"天使之城","descricao":"Um belo filme", "date":"30/11/2010 14:08:29"},{"id":"3010201012849669427","titulo":"天使之城","descricao":"Um belo filme","date" :"30/11/2010 14:08:49"},{"id":"3010201012919920893","titulo":"天使之城","descricao":"Um belo filme","date":"30 /11/2010 14:09:19"}] 给定类型 java.util.List 在com.google.gson.JsonDeserializerExceptionWrapper.deserialize(JsonDeserializerExceptionWrapper.java:63) 在 com.google.gson.JsonDeserializationVisitor.invokeCustomDeserializer(JsonDeserializationVisitor.java:88) 在 com.google.gson.JsonDeserializationVisitor.visitUsingCustomHandler(JsonDeserializationVisitor.java:76) 在 com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:106) 在 com.google.gson.JsonDeserializationContextDefault.fromJsonArray(JsonDeserializationContextDefault.java:64) 在com.google.gson.JsonDeserializationContextDefault.deserialize(JsonDeserializationContextDefault.java:49) 在 com.google.gson.Gson.fromJson(Gson.java:568) 在 com.google.gson.Gson.fromJson(Gson.java:515) 在 com.google.gson.Gson.fromJson(Gson.java:484) 在classes.Manip.receberMetaDados(Manip.java:64) 在servlet.OurTube_Servlet.buscar(OurTube_Servlet.java:105) 在 servlet.OurTube_Servlet.doPost(OurTube_Servlet.java:55) 在 javax.servlet.http.HttpServlet.service(HttpServlet.java:637) 在 javax.servlet.http.HttpServlet.service(HttpServlet.java:717) 在 org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) 在 org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) 在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) 在 org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) 在 org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) 在 org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) 在 org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298) 在 org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857) 在 org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588) 在 org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489) 在 java.lang.Thread.run(Thread.java:662) 引起原因:java.lang.RuntimeException:类classes.Video不存在的无参数构造函数。向 Gson 注册该类型的 InstanceCreator 可以解决此问题。 在 com.google.gson.MappedObjectConstructor.constructWithNoArgConstructor(MappedObjectConstructor.java:64) 在 com.google.gson.MappedObjectConstructor.construct(MappedObjectConstructor.java:53) 在 com.google.gson.JsonObjectDeserializationVisitor.constructTarget(JsonObjectDeserializationVisitor.java:40) 在 com.google.gson.JsonDeserializationVisitor.getTarget(JsonDeserializationVisitor.java:56) 在 com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:109) 在 com.google.gson.JsonDeserializationContextDefault.fromJsonObject(JsonDeserializationContextDefault.java:73) 在com.google.gson.JsonDeserializationContextDefault.deserialize(JsonDeserializationContextDefault.java:51) 在 com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter.deserialize(DefaultTypeAdapters.java:548) 在 com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter.deserialize(DefaultTypeAdapters.java:510) 在com.google.gson.JsonDeserializerExceptionWrapper.deserialize(JsonDeserializerExceptionWrapper.java:50) ... 25 更多
我不知道出了什么问题,谢谢你的帮助。
Does anybody know how can I send an object, to be more specific a List
, a result from a query in Database, to my servlet, which is another Java application and is not in Google App Engine.
Update: My servlet in GAE, is working fine, it serialize my List<Video>
result:
public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException {
String titulo = req.getParameter("titulo");
String json = "";
PersistenceManager pm = PMF.get().getPersistenceManager();
Query query = pm.newQuery("select from "+Video.class.getName()+ " where titulo.startsWith('"+titulo+"')");
List<Video> video = (List<Video>) pm.newQuery(query).execute();
json = new Gson().toJson(video);
System.out.println("SERIALIZED >> " + json);
res.setContentType("application/json");
res.setCharacterEncoding("UTF-8");
res.getWriter().write(json);
}
My calling servlet has this method:
public void receberMetaDados(String titulo) throws IOException, Exception{
InputStream input = new URL("http://localhost:8888/serve?titulo="+titulo).openStream();
Reader reader = new InputStreamReader(input, "UTF-8");
List<Video> results = new Gson().fromJson(reader, new TypeToken<List<Video>>(){}.getType());
}
I get the following exception:
com.google.gson.JsonParseException: The JsonDeserializer com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter@2d7440 failed to deserialized json object [{"id":"30102010113847691504","titulo":"CIty of Angel","descricao":"Um belo filme","date":"30/11/2010 13:38:47"},{"id":"30102010115514196289","titulo":"CIty of Angel","descricao":"Um belo filme","date":"30/11/2010 13:55:14"},{"id":"3010201011561620697","titulo":"CIty of Angel","descricao":"Um belo filme","date":"30/11/2010 13:56:01"},{"id":"3010201012829669834","titulo":"CIty of Angel","descricao":"Um belo filme","date":"30/11/2010 14:08:29"},{"id":"3010201012849669427","titulo":"CIty of Angel","descricao":"Um belo filme","date":"30/11/2010 14:08:49"},{"id":"3010201012919920893","titulo":"CIty of Angel","descricao":"Um belo filme","date":"30/11/2010 14:09:19"}] given the type java.util.List
at com.google.gson.JsonDeserializerExceptionWrapper.deserialize(JsonDeserializerExceptionWrapper.java:63)
at com.google.gson.JsonDeserializationVisitor.invokeCustomDeserializer(JsonDeserializationVisitor.java:88)
at com.google.gson.JsonDeserializationVisitor.visitUsingCustomHandler(JsonDeserializationVisitor.java:76)
at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:106)
at com.google.gson.JsonDeserializationContextDefault.fromJsonArray(JsonDeserializationContextDefault.java:64)
at com.google.gson.JsonDeserializationContextDefault.deserialize(JsonDeserializationContextDefault.java:49)
at com.google.gson.Gson.fromJson(Gson.java:568)
at com.google.gson.Gson.fromJson(Gson.java:515)
at com.google.gson.Gson.fromJson(Gson.java:484)
at classes.Manip.receberMetaDados(Manip.java:64)
at servlet.OurTube_Servlet.buscar(OurTube_Servlet.java:105)
at servlet.OurTube_Servlet.doPost(OurTube_Servlet.java:55)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:857)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Thread.java:662)
Caused by: java.lang.RuntimeException: No-args constructor for class classes.Video does not exist. Register an InstanceCreator with Gson for this type to fix this problem.
at com.google.gson.MappedObjectConstructor.constructWithNoArgConstructor(MappedObjectConstructor.java:64)
at com.google.gson.MappedObjectConstructor.construct(MappedObjectConstructor.java:53)
at com.google.gson.JsonObjectDeserializationVisitor.constructTarget(JsonObjectDeserializationVisitor.java:40)
at com.google.gson.JsonDeserializationVisitor.getTarget(JsonDeserializationVisitor.java:56)
at com.google.gson.ObjectNavigator.accept(ObjectNavigator.java:109)
at com.google.gson.JsonDeserializationContextDefault.fromJsonObject(JsonDeserializationContextDefault.java:73)
at com.google.gson.JsonDeserializationContextDefault.deserialize(JsonDeserializationContextDefault.java:51)
at com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter.deserialize(DefaultTypeAdapters.java:548)
at com.google.gson.DefaultTypeAdapters$CollectionTypeAdapter.deserialize(DefaultTypeAdapters.java:510)
at com.google.gson.JsonDeserializerExceptionWrapper.deserialize(JsonDeserializerExceptionWrapper.java:50)
... 25 more
I don't know what is wrong, thank you by your assist.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这要看情况。如果要作为 HTTP 请求的 HTTP 响应返回,则需要将其转换为遵循特定格式的字符串。流行的是 XML 和 JSON。另一端只需读取 XML 或 JSON 字符串,然后根据此信息重新创建列表。
下面是一个使用 JSON 和 Google Gson 的示例,它能够将 Java 对象转换为 JSON 并反之亦然。
被调用的 servlet:
调用 servlet:
更新:根据异常:
这是不言自明的。提供默认构造函数。
That depends. If it is to be returned as a HTTP response on a HTTP request, then you need to convert it to a string following a specific format. The popular ones are XML and JSON. The other end has just to read the XML or JSON string in and then recreate the list based on this information.
Here's an example with JSON and Google Gson which is able to convert Java objects to JSON and vice versa.
The called servlet:
The calling servlet:
Update: as per the exception:
This is pretty self-explaining. Supply a default constructor.