简体   繁体   English

来自vividsolutions JTS的几何图形在创建JSON时失败

[英]Geometry from vividsolutions JTS fails when creating JSON

pals. 好朋友。 I'm using vividsolutions's library JTS (1.13) for Points and Polygons in my application, but when I try to convert geometry objects into JSON, my application fails. 我在我的应用程序中使用了vividsolutions的库JTS(1.13)for Points and Polygons,但是当我尝试将几何对象转换为JSON时,我的应用程序失败了。 There is my source: 有我的来源:

@RequestMapping(value = "/test_point", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
@ResponseBody
public Map<String, Object> testPoint() {

    Point point = geometryFactory.createPoint(new Coordinate(37.73, 60.45));

    return RequestMapBuilder.getInstance()
            .addAttribute("point", point)
            .build();
}

And there is JSON I recieved by client: 我收到了JSON的客户:

{"point":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope":{"envelope": AND SO ON...

Also my application throws this exception: 我的应用程序也抛出此异常:

java.lang.IllegalStateException: Cannot call sendError() after the response has been committed
at org.apache.catalina.connector.ResponseFacade.sendError(ResponseFacade.java:478)
at javax.servlet.http.HttpServletResponseWrapper.sendError(HttpServletResponseWrapper.java:129)
at javax.servlet.http.HttpServletResponseWrapper.sendError(HttpServletResponseWrapper.java:129)
at org.springframework.security.web.context.SaveContextOnUpdateOrErrorResponseWrapper.sendError(SaveContextOnUpdateOrErrorResponseWrapper.java:92)
at org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver.sendServerError(DefaultHandlerExceptionResolver.java:313)
at org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver.handleHttpMessageNotWritable(DefaultHandlerExceptionResolver.java:370)
at org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver.doResolveException(DefaultHandlerExceptionResolver.java:140)
at org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver.resolveException(AbstractHandlerExceptionResolver.java:138)
at org.springframework.web.servlet.DispatcherServlet.processHandlerException(DispatcherServlet.java:1183)
at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1020)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:971)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:857)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:316)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:126)
at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:90)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:114)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:122)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter.doFilter(RememberMeAuthenticationFilter.java:149)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:168)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:48)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:205)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:120)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:64)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:53)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:91)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:213)
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:176)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:344)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:261)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:610)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:668)
at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:223)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1517)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1474)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:744)

Any ideas what is wrong? 有什么想法有什么不对? Thanks in advance. 提前致谢。

If you're using Spring Boot, this is enough: 如果您使用的是Spring Boot,这就足够了:

pom.xml : pom.xml

<dependency>
    <groupId>com.bedatadriven</groupId>
    <artifactId>jackson-datatype-jts</artifactId>
    <version>2.3</version>
</dependency>

JacksonConfig.java : JacksonConfig.java

package org.lskk.lumen.helpdesk;

import com.bedatadriven.jackson.datatype.jts.JtsModule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class JacksonConfig {
    @Bean
    public JtsModule jtsModule() {
        return new JtsModule();
    }
}

This is not very nice solution, but it helps me to solve issue. 这不是一个很好的解决方案,但它有助于我解决问题。

So, issue is in Jackson way to serialize JTS Polygon and Point objects. 因此,问题是以Jackson的方式序列化JTS Polygon和Point对象。 This issue is solved by project jackson-datatype-jts. 此问题由项目jackson-datatype-jts解决。 You can show this library sources on github - https://github.com/bedatadriven/jackson-datatype-jts 您可以在github上显示此库源代码 - https://github.com/bedatadriven/jackson-datatype-jts

All I need to run my test_point is: 我需要运行我的test_point是:

1) Add dependency and repository for jackson-datatype-jts: 1)为jackson-datatype-jts添加依赖项和存储库:

<dependencies>
    <dependency>
        <groupId>com.bedatadriven</groupId>
        <artifactId>jackson-datatype-jts</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>

</dependencies>

<repositories>
    <repository>
        <id>sonatype-oss</id>
        <url>https://oss.sonatype.org/content/groups/public</url>
    </repository>
</repositories>

2) Change source code this way: 2)以这种方式更改源代码:

@RequestMapping(value = "/test_point", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
@ResponseBody
public Point testPoint() {

    Point point =geometryFactory.createPoint(new Coordinate(37.77, 60.01));
    String result = "";
    ObjectMapper objectMapper = new ObjectMapper();
    objectMapper.registerModule(new JtsModule());
    try {
        result = objectMapper.writeValueAsString(point);
    } catch (JsonProcessingException e) {
        e.printStackTrace();
    }

    return point;
}

And this really works, but what is I want to return point in my other POJO? 这确实有效,但我想在其他POJO中返回什么? There is solution for Spring Framework: 1) Repeat step 1 from previous section 2) Create a class extending ObjectMapper Class: 有Spring Framework的解决方案:1)重复上一节中的步骤1 2)创建一个扩展ObjectMapper类的类:

public class CustomMapper extends ObjectMapper {

public CustomMapper() {
    super();
    this.registerModule(new JtsModule());
}
}

3) Edit your spring config xml (for me - servlet.xml) add next strings: 3)编辑spring config xml(对我来说 - servlet.xml)添加下一个字符串:

<mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
            <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"/>
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                <property name="objectMapper" ref="jacksonObjectMapper" />
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>

    <bean id="jacksonObjectMapper" class="ru.trollsmedjan.web.dao.CustomMapper"/>

Now your ObjectMapper instance is substituted with your Custom Object mapper realization. 现在,您的ObjectMapper实例将替换为您的自定义对象映射器实现。 An it works! 它有效!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 com.vividsolutions.jts.geom.Geometry类中的距离单位是多少? - What are the distance units in com.vividsolutions.jts.geom.Geometry class? 如何使用休眠会话将oracle空间SDO_GEOMETRY转换为com.vividsolutions.jts.geom.Geometry - how to fectch oracle spatial SDO_GEOMETRY as com.vividsolutions.jts.geom.Geometry using hibernate session com.vividsolutions.jts 和 org.locationtech.jts 之间的转换器 - Converters between com.vividsolutions.jts and org.locationtech.jts 是否有Java的几何库? (不是JTS) - Is there a geometry library for Java? (not JTS) 如何在JTS中从纬度,经度到经度,纬度交换jts.geom.Geometry对象的坐标 - How to Swap Coordinates of jts.geom.Geometry object from Lat, Long to Long,Lat in JTS Geotools com.vividsolutions.jts.geom.TopologyException:侧面位置冲突 - Geotools com.vividsolutions.jts.geom.TopologyException: side location conflict 用休眠5保存vividsolutions.jts.geom.Polygonal属性 - Save vividsolutions.jts.geom.Polygonal attribute with hibernate 5 映射到 com.vividsolutions.jts.geom.Point 无效端标志 - Mapping to com.vividsolutions.jts.geom.Point invalid endian flag 将 com.vividsolutions.jts 迁移到 org.locationtech.jts 仍然抱怨缺少 com.vividsolutions package - Migrating com.vividsolutions.jts to org.locationtech.jts still complaining about lack of com.vividsolutions package 在JTS中,当我执行geometry.buffer(1).buffer(-1)时,生成的几何形状是否等于原始几何形状? - In JTS, when I do geometry.buffer(1).buffer(-1), will the resulting geometry be equivalent to the original?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM