带JSON的App Engine(泽西岛)

时间:2014-03-08 13:48:51

标签: java json google-app-engine jersey gson

我正在尝试生成JSON(MediaType.APPLICATION_JSON)。 如果我使用MediaType.APPLICATION_JSON,我输出错误。 但每当我使用text / plain时,我都会得到正确的输出。 我想使用MediaType.APPLICATION_JSON来设置正确的标题。

这段代码是否正确? (我正在使用Jersey + App引擎)

@Path("all")
@GET
@Produces(MediaType.APPLICATION_JSON)
public String getAllPatients(){
    Patient x = new Patient();
    x.setNaam("david");
    String test = gson.toJson(x);
    return test;
}

这是错误的输出:

"{\"naam\":\"david\",\"leeftijd\":0}"

编辑:

它与XML(MediaType.APPLICATION_XML)完美配合,但在尝试使用JSON时出现以下错误:

java.lang.NullPointerException
at com.owlike.genson.reflect.BeanViewDescriptorProvider$BeanViewPropertyFactory.createAccessor(BeanViewDescriptorProvider.java:122)
at com.owlike.genson.reflect.BeanPropertyFactory$CompositeFactory.createAccessor(BeanPropertyFactory.java:49)
at com.owlike.genson.reflect.BaseBeanDescriptorProvider.provideMethodAccessors(BaseBeanDescriptorProvider.java:187)
at com.owlike.genson.reflect.BaseBeanDescriptorProvider.provideBeanPropertyAccessors(BaseBeanDescriptorProvider.java:94)
at com.owlike.genson.reflect.AbstractBeanDescriptorProvider.provide(AbstractBeanDescriptorProvider.java:106)
at com.owlike.genson.convert.BasicConvertersFactory.provide(BasicConvertersFactory.java:109)
at com.owlike.genson.convert.BasicConvertersFactory.create(BasicConvertersFactory.java:74)
at com.owlike.genson.convert.BasicConvertersFactory.create(BasicConvertersFactory.java:56)
at com.owlike.genson.reflect.AbstractBeanDescriptorProvider$ContextualFactoryDecorator.create(AbstractBeanDescriptorProvider.java:79)
at com.owlike.genson.reflect.AbstractBeanDescriptorProvider$ContextualFactoryDecorator.create(AbstractBeanDescriptorProvider.java:68)
at com.owlike.genson.convert.ChainedFactory.create(ChainedFactory.java:93)
at com.owlike.genson.convert.ChainedFactory.create(ChainedFactory.java:80)
at com.owlike.genson.convert.ChainedFactory.create(ChainedFactory.java:93)
at com.owlike.genson.convert.ChainedFactory.create(ChainedFactory.java:80)
at com.owlike.genson.convert.ChainedFactory.create(ChainedFactory.java:93)
at com.owlike.genson.convert.ChainedFactory.create(ChainedFactory.java:80)
at com.owlike.genson.convert.CircularClassReferenceConverterFactory.create(CircularClassReferenceConverterFactory.java:58)
at com.owlike.genson.convert.CircularClassReferenceConverterFactory.create(CircularClassReferenceConverterFactory.java:22)
at com.owlike.genson.Genson.provideConverter(Genson.java:196)
at com.owlike.genson.Genson.serialize(Genson.java:340)
at com.owlike.genson.ext.jaxrs.GensonJsonConverter.writeTo(GensonJsonConverter.java:91)
at com.sun.jersey.spi.container.ContainerResponse.write(ContainerResponse.java:289)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1029)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:941)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:932)
at com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:384)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:451)
at com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:632)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166)
at com.google.appengine.api.socket.dev.DevSocketFilter.doFilter(DevSocketFilter.java:74)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.tools.development.ResponseRewriterFilter.doFilter(ResponseRewriterFilter.java:123)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.tools.development.HeaderVerificationFilter.doFilter(HeaderVerificationFilter.java:34)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.api.blobstore.dev.ServeBlobFilter.doFilter(ServeBlobFilter.java:63)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:43)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.tools.development.StaticFileFilter.doFilter(StaticFileFilter.java:125)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.tools.development.DevAppServerModulesFilter.doDirectRequest(DevAppServerModulesFilter.java:366)
at com.google.appengine.tools.development.DevAppServerModulesFilter.doDirectModuleRequest(DevAppServerModulesFilter.java:349)
at com.google.appengine.tools.development.DevAppServerModulesFilter.doFilter(DevAppServerModulesFilter.java:116)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
at com.google.appengine.tools.development.DevAppEngineWebAppContext.handle(DevAppEngineWebAppContext.java:97)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at com.google.appengine.tools.development.JettyContainerService$ApiProxyHandler.handle(JettyContainerService.java:487)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:326)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:923)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:547)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:212)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:409)
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)

3 个答案:

答案 0 :(得分:1)

尝试使用json.org中的json时遇到了同样的问题。我从source forge http://json-lib.sourceforge.net/切换到jar,其中包括net.sf.json.JSONObject,它可以工作。不知道为什么json.org的版本不起作用。我计划进一步研究它,因为我被要求使用标准实现。我也在使用genson-0.97.jar。

更新: 使用json.org,只需将其作为String.class返回,它就可以填充JSONObject。

对于单个返回值:

JSONObject jsonObj = new JSONObject(response.readEntity(String.class));

对于一组值:

JSONArray jsonArray = new JSONArray(response.readEntity(String.class));

客户端也可以接收它作为HashMap.class类型:

HashMap<String,String> resultsMap = response.readEntity(HashMap.class);

服务实际上将参数作为HashMap接收:

@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public Response getServiceMethod(HashMap<String,String> requestAttributes) {
    ...
    Response finalResponse = Response.ok(somePojoCollection, MediaType.APPLICATION_JSON).build();
    return finalResponse;
}

完整客户端示例:

private JSONObject executeService(AuthRequest inputAttributes) {
    Client client = ClientBuilder.newClient();
    WebTarget target = client.target("http://localhost:8080").path(
        "TestRestService2/rest/GetTestService");
    Entity<AuthRequest> entity = Entity.entity(inputAttributes,
            MediaType.APPLICATION_JSON);
    Builder request = target.request(MediaType.APPLICATION_JSON);
    Response response = request.buildPost(entity).invoke();
    JSONArray jsonArray = new JSONArray(response.readEntity(String.class));
    JSONObject json = (JSONObject)jsonArray.get(0);
    return json;
}

还可以通过客户端传递JSONObject,但如果使用json.org,则使用toString()

JSONObject inputAttributes = new JSONObject(myHashMap);
Entity<String> entity = Entity.entity(inputAttributes.toString(),
                       MediaType.APPLICATION_JSON);

答案 1 :(得分:0)

Jersey可以自己处理JSON,只需设置正确的库依赖项:jersey-json.jar。请参阅此example

在这种情况下,你直接返回Patient,泽西将处理其余的事情:

@Path("all")
@GET
@Produces(MediaType.APPLICATION_JSON)
public Patient getAllPatients(){
    Patient x = new Patient();
    x.setNaam("david");
    return x;
}

答案 2 :(得分:0)

不是手动进行序列化,而是让泽西岛处理它。更改您的方法以返回患者而不是字符串,这就是全部。你不应该再有周围的引号了。