This is not a good solution, but it helps me solve the problem.
So, the problem is Jackson's method for serializing polygon objects and JTS objects. This problem is solved by the jackson-datatype-jts project. You can show these library sources on github - https://github.com/bedatadriven/jackson-datatype-jts
All I need to run my test point:
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 the source code as follows:
@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 it really works, but what do I want to return in another POJO? There is a solution for the Spring Framework: 1) Repeat step 1 from the previous section 2) Create a class that extends the 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 the following lines:
<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 replaced by the implementation of your custom object. He works!
source share