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. Ther
If you're using Spring Boot, this is enough:
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();
}
}
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. This issue is solved by project jackson-datatype-jts. You can show this library sources on github - https://github.com/bedatadriven/jackson-datatype-jts
All I need to run my test_point is:
1) Add dependency and repository for 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:
@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? There is solution for Spring Framework: 1) Repeat step 1 from previous section 2) Create a class extending ObjectMapper Class:
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:
<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. An it works!