Java 8 Jackson不可变类反序列化

问题描述 投票:2回答:1

我正在尝试将JSON字符串反序列化为仅具有所有参数构造函数和getter的java pojo类。所有字段都是最终字段,并且该类是不可变的。这是我的java pojo对象:

Foo.java

public class Foo {
    private final int id;
    private final int cId;
    private final String cName;
    private final String code;
    private final String display;

    public Foo(final int id, 
               final int cId,
               final String cName,
               final String code,
               final String display) {
        this.id = id;
        this.cId = cId;
        this.cName = cName;
        this.code = code;
        this.display = display;
    }

    //Some copy constructor
    public Foo(final FooB fooB) {
        this.id = fooB.getId();
        this.cId = fooB.getCId();
        this.cName = fooB.getContractorCName();
        this.code = fooB.getCode();
        this.display = fooB.getDisplay();
    }

    public int getId() {
        return id;
    }

    public int getCId() {
        return cId;
    }

    public String getCName() {
        return cName;
    }

    public String getCode() {
        return code;
    }

    public String getDisplay() {
        return display;
    }
}

这是我的测试:

public static void main(String[] args) {
    ParameterNamesModule pnm = new ParameterNamesModule(JsonCreator.Mode.PROPERTIES);
    ObjectMapper mapper = new ObjectMapper().enable(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES).registerModule(pnm);
    String json = "{\"id\":19,\"cId\":13234,\"cName\":\"SOME NAME\",\"code\":\"8EJ4\",\"display\":\"SOME DISPLAY NAME\"}";
    try {
        mapper.readValue(json, Foo.class);
    } catch (JsonProcessingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

但是,当我运行它时,出现以下错误:

objc[58537]: Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/bin/java (0x109dbf4c0) and /Library/Java/JavaVirtualMachines/jdk1.8.0_121.jdk/Contents/Home/jre/lib/libinstrument.dylib (0x109e874e0). One of the two will be used. Which one is undefined.
com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Cannot construct instance of `com.test.Foo` (no Creators, like default construct, exist): cannot deserialize from Object value (no delegate- or property-based Creator)
 at [Source: (String)"{"id":19,"cId":13234,"cName":"SOME NAME","code":"8EJ4","display":"SOME DISPLAY NAME"}"; line: 1, column: 2]
    at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:67)
    at com.fasterxml.jackson.databind.DeserializationContext.reportBadDefinition(DeserializationContext.java:1451)
    at com.fasterxml.jackson.databind.DeserializationContext.handleMissingInstantiator(DeserializationContext.java:1027)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromObjectUsingNonDefault(BeanDeserializerBase.java:1290)
    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:4001)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2992)
    at JacksonCreator.main(JacksonCreator.java:18)

我做错了什么?我正在使用Jackson的2.9.4版本

java json serialization java-8 jackson
1个回答
0
投票

尝试一下

@JsonCreator(mode = JsonCreator.Mode.PROPERTIES) public Foo(@JsonProperty ("id") final int id, @JsonProperty ("cId") final int cId, ...) { this.id = id; this.cId = cId; this.cName = cName; this.code = code; this.display = display; }

© www.soinside.com 2019 - 2024. All rights reserved.