如何使用ObjectMapper在没有默认构造函数的情况下反序列化不可变对象? [英] How to de/serialize an immutable object without default constructor using ObjectMapper?

查看:559
本文介绍了如何使用ObjectMapper在没有默认构造函数的情况下反序列化不可变对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用com.fasterxml.jackson.databind.ObjectMapper对不可变对象进行序列化和反序列化.

I want to serialize and deserialize an immutable object using com.fasterxml.jackson.databind.ObjectMapper.

不可变类如下所示(只有3个内部属性,获取器和构造函数):

The immutable class looks like this (just 3 internal attributes, getters and constructors):

public final class ImportResultItemImpl implements ImportResultItem {

    private final ImportResultItemType resultType;

    private final String message;

    private final String name;

    public ImportResultItemImpl(String name, ImportResultItemType resultType, String message) {
        super();
        this.resultType = resultType;
        this.message = message;
        this.name = name;
    }

    public ImportResultItemImpl(String name, ImportResultItemType resultType) {
        super();
        this.resultType = resultType;
        this.name = name;
        this.message = null;
    }

    @Override
    public ImportResultItemType getResultType() {
        return this.resultType;
    }

    @Override
    public String getMessage() {
        return this.message;
    }

    @Override
    public String getName() {
        return this.name;
    }
}

但是,当我运行此单元测试时:

However when I run this unit test:

@Test
public void testObjectMapper() throws Exception {
    ImportResultItemImpl originalItem = new ImportResultItemImpl("Name1", ImportResultItemType.SUCCESS);
    String serialized = new ObjectMapper().writeValueAsString((ImportResultItemImpl) originalItem);
    System.out.println("serialized: " + serialized);

    //this line will throw exception
    ImportResultItemImpl deserialized = new ObjectMapper().readValue(serialized, ImportResultItemImpl.class);
}

我收到此异常:

com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class eu.ibacz.pdkd.core.service.importcommon.ImportResultItemImpl]: can not instantiate from JSON object (missing default constructor or creator, or perhaps need to add/enable type information?)
 at [Source: {"resultType":"SUCCESS","message":null,"name":"Name1"}; line: 1, column: 2]
    at 
... nothing interesting here

此异常要求我创建一个默认的构造函数,但这是一个不可变的对象,所以我不想拥有它.它将如何设置内部属性?这会完全混淆API的用户.

This exception asks me to create a default constructor, but this is an immutable object, so I don't want to have it. How would it set the internal attributes? It would totally confuse the user of the API.

所以我的问题是:我可以在没有默认构造函数的情况下以某种方式反序列化不可变对象吗?

So my question is: Can I somehow de/serialize immutable objects without default constructor?

推荐答案

要让Jackson知道如何创建对象以进行反序列化,请使用 @JsonProperty 批注,如下所示:

To let Jackson know how to create an object for deserialization, use the @JsonCreator and @JsonProperty annotations for your constructors, like this:

@JsonCreator
public ImportResultItemImpl(@JsonProperty("name") String name, 
        @JsonProperty("resultType") ImportResultItemType resultType, 
        @JsonProperty("message") String message) {
    super();
    this.resultType = resultType;
    this.message = message;
    this.name = name;
}

这篇关于如何使用ObjectMapper在没有默认构造函数的情况下反序列化不可变对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆