2015-04-26 11 views
5

. Używam biblioteki JTS (1.13) vividsolutions dla punktów i wielokątów w mojej aplikacji, ale kiedy próbuję konwertować obiekty geometrii do JSON, moja aplikacja zawiedzie. Jest moje źródło:Geometria od vividsolutions JTS kończy się niepowodzeniem podczas tworzenia kumpli JSON

@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(); 
} 

I jest JSON otrzymywałem przez klienta:

{"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... 

Również moja aplikacja generuje ten wyjątek:

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) 

jakieś pomysły co jest nie tak? Z góry dziękuję.

Odpowiedz

2

To nie jest dobre rozwiązanie, ale pomaga mi rozwiązać problem.

Tak, problem jest w Jackson sposób serializowania obiektów wieloboków i punktów JTS. Ten problem rozwiązuje projekt jackson-datatype-jts. Możesz pokazać ten źródeł bibliotecznych na github - https://github.com/bedatadriven/jackson-datatype-jts

Wszystko muszę uruchomić mój test_point jest:

1) Dodaj zależność i repozytorium Jackson typów danych-WST:

<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) Zmień kod źródłowy w następujący sposób:

@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; 
} 

A to naprawdę działa, ale co chcę zwrócić w innym POJO? Jest rozwiązaniem dla Spring Framework: 1) Powtórz krok 1 z poprzedniej części 2) Utwórz klasę rozciągający ObjectMapper Klasa:

public class CustomMapper extends ObjectMapper { 

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

3) Edycja sprężyny konfiguracyjny xml (dla mnie - servlet.xml) dodać następny ciągi:

<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"/> 

Teraz twoja instancja ObjectMapper zostaje zastąpiona realizacją mapera obiektów niestandardowych. To działa!

2

Jeśli używasz Wiosna Boot, to wystarczy:

pom.xml:

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

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(); 
    } 
} 
+1

Ten pracował dla mnie ostatecznym wyjściu jako '{ "type" : "Punkt", "współrzędne": [1,0,1.0]} " – rockoder