杰克逊JSON视图与几何

时间:2013-01-14 00:41:11

标签: hibernate geometry jackson spatial

我在具有Geometry的PostgreSQL DB + PostGIS上使用Spring + Hibernate,它在DB上执行CRUD操作并使用Jackson JSON视图返回JSON输出。在我添加Geometry之前,它工作正常。当我检查对象时,搜索返回正确的输出,但是当它创建具有Geometry的结果的JSON表示时似乎存在问题。我得到这样的东西:

org.springframework.http.converter.HttpMessageNotWritableException: Could not write JSON: This method does not support GeometryCollection arguments (through reference chain: java.util.ArrayList[0]->com.tutorial.jquery.model.State["geom"]->com.vividsolutions.jts.geom.MultiPolygon["boundary"]->com.vividsolutions.jts.geom.MultiLineString["boundary"]->com.vividsolutions.jts.geom.MultiPoint["boundary"]->com.vividsolutions.jts.geom.GeometryCollection["boundary"]); nested exception is org.codehaus.jackson.map.JsonMappingException: This method does not support GeometryCollection arguments (through reference chain: java.util.ArrayList[0]->com.tutorial.jquery.model.State["geom"]->com.vividsolutions.jts.geom.MultiPolygon["boundary"]->com.vividsolutions.jts.geom.MultiLineString["boundary"]->com.vividsolutions.jts.geom.MultiPoint["boundary"]->com.vividsolutions.jts.geom.GeometryCollection["boundary"])
at org.springframework.http.converter.json.MappingJacksonHttpMessageConverter.writeInternal(MappingJacksonHttpMessageConverter.java:195)
at org.springframework.http.converter.AbstractHttpMessageConverter.write(AbstractHttpMessageConverter.java:179)
at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:148)
at org.springframework.web.servlet.mvc.method.annotation.AbstractMessageConverterMethodProcessor.writeWithMessageConverters(AbstractMessageConverterMethodProcessor.java:90)
at org.springframework.web.servlet.mvc.method.annotation.RequestResponseBodyMethodProcessor.handleReturnValue(RequestResponseBodyMethodProcessor.java:189)
at org.springframework.web.method.support.HandlerMethodReturnValueHandlerComposite.handleReturnValue(HandlerMethodReturnValueHandlerComposite.java:69)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:122)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:746)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:687)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:925)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:915)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:811)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:796)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:390)
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 org.mortbay.jetty.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:230)
at org.mortbay.jetty.handler.HandlerCollection.handle(HandlerCollection.java:114)
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)
Caused by: org.codehaus.jackson.map.JsonMappingException: This method does not support GeometryCollection arguments (through reference chain: java.util.ArrayList[0]->com.tutorial.jquery.model.State["geom"]->com.vividsolutions.jts.geom.MultiPolygon["boundary"]->com.vividsolutions.jts.geom.MultiLineString["boundary"]->com.vividsolutions.jts.geom.MultiPoint["boundary"]->com.vividsolutions.jts.geom.GeometryCollection["boundary"])
at org.codehaus.jackson.map.JsonMappingException.wrapWithPath(JsonMappingException.java:215)
at org.codehaus.jackson.map.JsonMappingException.wrapWithPath(JsonMappingException.java:180)
at org.codehaus.jackson.map.ser.SerializerBase.wrapAndThrow(SerializerBase.java:128)

有人可以提出建议吗?

2 个答案:

答案 0 :(得分:0)

我遇到以下错误,其他后续错误隐藏了该错误:

org.codehaus.jackson.map.JsonMappingException: This method does not 
support GeometryCollection arguments (through reference chain: 
com.mapflow.exposure.model.wrapper.DefaultWrapper["result"]->
java.util.ArrayList[0]->com.mapflow.exposure.model.poi.SimplePoint["geom"]->
com.vividsolutions.jts.geom.Point["boundary"]->
com.vividsolutions.jts.geom.GeometryCollection["boundary"]) 

原因看起来我们在我们的Weblogic环境中使用了Jackson库(我们在Tomcat中使用了另一个工作正常)并且它无法处理几何属性。

我们的工作是将@JsonIgnore添加到实体及其getter中的属性声明中:

import org.codehaus.jackson.annotate.JsonIgnore;
@JsonIgnore
private Geometry geom;
(...)
@JsonIgnore
public Geometry getGeom() {
    return geom;
}

我可以在这个问题中看到其他更精细的方法,但我自己没有尝试过:

Geometry from vividsolutions JTS fails when creating JSON

答案 1 :(得分:0)

您可以为 jackson 的几何类型注册自定义序列化程序,例如参见 Custom Jackson Serializer for a specific type in a particular class。问题是,您希望它如何呈现为 json?通过 WKT 还是其他什么?