Jackson序列化org.locationtech.jts.geom.Point

本文介绍使用Jackson序列化JTS Point对象时遇到的栈溢出问题及解决方案,通过引入第三方扩展jackson-datatype-jts并注册模块实现正确的GeoJSON序列化。
部署运行你感兴趣的模型镜像

当使用Jackson来序列化 org.locationtech.jts.geom.Point 对象,就会发生递归的栈溢出

GeometryFactory gf = new GeometryFactory();
Point point = gf.createPoint(new Coordinate(1.2345678, 2.3456789));
String geojson = objectMapper.writeValueAsString(point);

如下:

com.fasterxml.jackson.databind.JsonMappingException: Infinite recursion (StackOverflowError) (through reference chain:
org.locationtech.jts.geom.Point["envelope"]->org.locationtech.jts.geom.Point["envelope"]->org.locationtech.jts.geom.Point["envelope"]
->org.locationtech.jts.geom.Point["envelope"]->org.locationtech.jts.geom.Point["envelope"]
->org.locationtech.jts.geom.Point["envelope"]->org.locationtech.jts.geom.Point["envelope"]
->org.locationtech.jts.geom.Point["envelope"]-
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:689)
at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:157)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:693)

这是由于发生了递归调用,jackson不能很好的处理,好在jackson的可扩展性不错,于是出了一些第三方扩展解决这个问题。

比如这位:https://github.com/desoss/jackson-datatype-jts

我们引入其依赖:

 <repository>
	<id>jitpack.io</id>
	<url>https://jitpack.io</url>
</repository>

<dependency>
	<groupId>com.github.desoss</groupId>
	<artifactId>jackson-datatype-jts</artifactId>
	<version>2.5.1.2</version>
</dependency>

然后注册新的module:

objectMapper = new ObjectMapper();
objectMapper.registerModule(new JtsModule());

就可以正确序列化了,源码也就十几个类,可以自己按想要的格式修改。

最后,整个Geometry的实现类都是同样的道理。

您可能感兴趣的与本文相关的镜像

Llama Factory

Llama Factory

模型微调
LLama-Factory

LLaMA Factory 是一个简单易用且高效的大型语言模型(Large Language Model)训练与微调平台。通过 LLaMA Factory,可以在无需编写任何代码的前提下,在本地完成上百种预训练模型的微调

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值