Google App Engine - How return an object to my servlet?
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$CollectionTypeAd开发者_运维技巧apter@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.
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:
List<Result> results = someDAO.list();
String json = new Gson().toJson(list);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(json);
The calling servlet:
InputStream input = new URL("http://example.com/servleturl").openStream();
Reader reader = new InputStreamReader(input, "UTF-8");
List<Result> results = new Gson().fromJson(reader, new TypeToken<List<Result>>(){}.getType());
Update: as per the exception:
Caused by: java.lang.RuntimeException: No-args constructor for class classes.Video does not exist.
This is pretty self-explaining. Supply a default constructor.
public class Video {
public Video() {
// Always keep default c'tor alive whenever you
// supply another c'tor in a Javabean class.
}
}
精彩评论