我正在尝试使用Jackson序列化/反序列化DynamoDB V2 AttributeValue类。
它被设置为一个具有Builder的不可变类,并且Builder有一个私有构造函数。为了创建一个构建器,需要调用AttributeValue.builder()
。
我无法控制这个类,所以我想使用Jacksonmixins。
我使用了@JsonDeserialize(builder = AttributeValue.Builder::class)
并注册了mixin:
@JsonDeserialize(builder = AttributeValue.Builder::class)
interface AttributeValueMixin {
}
private val mapper = jacksonObjectMapper()
.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY)
.addMixIn(AttributeValue::class.java, AttributeValueMixin::class.java)
但是,Jackson试图使用AttributeValue.Builder
的默认构造函数,但由于没有,因此无法使用。
com.fasterxml.jackson.databind.exc.InvalidDefinitionException:无法构造software.amazon.awssdk.services.dynamodb.model.AttributeValue$Builder
的示例(不存在创建者,如默认构造)
如何让Jackson使用AttributeValue.builder()
工厂函数?关于如何使用Jackson来序列化/反序列化这个AttributeValue
类,有什么其他想法吗?
3条答案
按热度按时间uujelgoq1#
确实很棘手我能想到两个解决办法:
1.在原始构建器周围创建 Package :
1.如果你是直接调用对象Map器,你可以尝试以下技巧:
zaq34kh62#
所以这感觉完全是垃圾,但它的工作,所以。.. ¯*()*/¯
在我的例子中,我需要序列化/沙漠化一个
Map<String, AttributeValue>
,所以我使用了一种技术,将Map的JSON版本填充到一个“map”属性中,反序列化,然后提取“map”值:mgdq6dx13#
请看我对这个问题的回答:https://stackoverflow.com/a/65603336/2288986
摘要
首先是一个helper方法:
然后为你的类添加
ValueInstantiator
:现在Jackson将能够示例化
AttributeValue.Builder
。