无法使用Final Field Bu Jackson反序列化最简单的对象

时间:2019-03-24 11:28:14

标签: java jackson

我只想序列化和反序列化简单的不可变对象,却不明白为什么我不能使用Jackson来实现

import java.io.IOException;

import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Value;

public class TestApplication {

    @Value
    static class Test {
        private final String a;
    }

    public static void main(String[] args) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();

        String res = objectMapper.writeValueAsString(new Test("test"));
        System.out.println(res);

        System.out.println(objectMapper.readValue(res, Test.class));
    }
}

失败,但出现以下异常:

Exception in thread "main" com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `com.devchallange.rogatakopita.RogatakopitaApplication$Test` (although at least one Creator exists): cannot deserialize from Object value (no delegate- or property-based Creator)
 at [Source: (String)"{"a":"test"}"; line: 1, column: 2]
    at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:63)
    at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1343)
    at com.fasterxml.jackson.databind.DeserializationContext.handleMissingInstantiator(DeserializationContext.java:1032)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1297)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:326)
    at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:159)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:4013)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3004)
    at com.devchallange.rogatakopita.RogatakopitaApplication.main(RogatakopitaApplication.java:29)

我知道答案应该很简单,但这就像最普通的情况一样,不是吗?

2 个答案:

答案 0 :(得分:1)

在设计不可变类时,Jackson应该使用所有必需的参数调用构造函数。

如果您不使用lombok,则

  • @JsonCreator注释一下您希望Jackson调用的构造函数。
  • @JsonProperty注释构造函数参数。如果要跳过此操作,可以添加使用ParameterNamesModule扩展名。

示例:

static class Test {

    private final String a;

    public Test() {
        a = "default";
    }

    @JsonCreator // Jackson will use this constructor during deserialization
    public Test(@JsonProperty("a") String a) { // @JsonProperty can be skipped if you use ParameterNamesModule annotation
        this.a = a;
    }

    // Getter for A
}

Lombok的@Value注释仅生成一个单一的all-args构造函数,我们需要使用@JsonCreator进行注释。这可以通过用@AllArgsConstructor(onConstructor = @__(@JsonCreator))注释类来完成。

由于构造函数是自动生成的,因此我们将无法使用@JsonProperty注释参数,因此,您将不得不使用ParameterNamesModule

具有以下更改的代码段:

public class TestApplication {

    @Value
    @AllArgsConstructor(onConstructor = @__(@JsonCreator))
    static final class Test {
        private final String a;
    }

    public static void main(String[] args) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.registerModule(new ParameterNamesModule());

        String res = objectMapper.writeValueAsString(new Test("test"));
        System.out.println(res);
        System.out.println(objectMapper.readValue(res, Test.class));
    }
}

以下是参数名称模块的maven依赖项。

 <dependency>
      <groupId>com.fasterxml.jackson.module</groupId>
      <artifactId>jackson-module-parameter-names</artifactId>
      <version>2.9.8</version>
      <scope>compile</scope>
 </dependency>

答案 1 :(得分:0)

在Java中,必须在构造函数中设置final属性,并且只能在以后读取。 但是杰克逊后来尝试更改该属性。因此,您无法使用杰克逊填写最终字段。