gpt4 book ai didi

Java 8 Jackson 不可变类反序列化

转载 作者:行者123 更新时间:2023-12-05 06:36:22 36 4
gpt4 key购买 nike

我正在尝试将 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)

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

最佳答案

试试这个

@JsonCreator(模式 = JsonCreator.Mode.PROPERTIES)
public Foo(@JsonProperty ("id") 最终 int id,
@JsonProperty ("cId") final int cId,
...){
这个.id = id;
这个.cId = cId;
...
}

关于Java 8 Jackson 不可变类反序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49141206/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com