JacksonによるJsonの逆シリアル化について質問があります(編集:2.0.4バージョン)。他のBeanのリストを含むBeanを文字列としてシリアル化し、この文字列を保存してから、後でこの文字列を逆シリアル化します。私はいくつかの基本クラスとそのサブタイプを使用します。基本クラスParentは抽象クラスであり、getterとsetterの2つの属性があり、このクラスには抽象メソッドgetType()もあります。他の抽象クラスAbstractChildは、クラスParentから継承します。このクラスにも属性があり、isExportEnabled()抽象メソッドがあります。このBeanがシリアル化されれば問題ありません。親に次の注釈を使用しますclass * @ JsonTypeInfo(use = JsonTypeInfo.Id.MINIMAL_CLASS、include = JsonTypeInfo.As.PROPERTY、property = "@cls")*文字列が生成されます。しかし、desirializeは失敗しました。例外「Unrecognizedfield"type"」がスローされます。しかし、私はこの属性が必要です!抽象メソッドに@JsonProperty( "type")を設定しようとしましたが、効果がありません。私を助けてください。
編集:プライベートフィールド「type」(親)と「exportEnabled」(AbstractChild)を導入すると、正しく実行されます。 PS例外
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException:認識されないフィールド "type"(class tst.SimpleTestMain $ FirstChild)、無視可能としてマークされていません(4つの既知のプロパティ:、 "id"、 "maxCount"、 "code"、 "minCount "])[ソース:java.io.StringReader@1ad9fa; 行:1、列:125](参照チェーンを介して:tst.Fam ["members"]-> tst.FirstChild ["type"])at com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException.from(UnrecognizedPropertyException.java :79)com.fasterxml.jackson.databind.DeserializationContext.reportUnknownProperty(DeserializationContext.java:568)で
…そしてサンプルクラス
package tst;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.ObjectMapper;
public class SimpleTestMain {
enum Type {
TYPE_A, TYPE_B
}
@JsonTypeInfo(use = JsonTypeInfo.Id.MINIMAL_CLASS, include = JsonTypeInfo.As.PROPERTY, property = "@cls")
public static abstract class Parent {
private int id;
private String code;
public Parent() {
}
@JsonProperty("type")
// First abstract getter
public abstract Type getType();
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
}
public static abstract class AbstractChild extends Parent {
private int minCount;
private int maxCount;
public AbstractChild() {
}
// Second abstract method: boolean used
public abstract boolean isExportEnabled();
public int getMinCount() {
return minCount;
}
public void setMinCount(int minCount) {
this.minCount = minCount;
}
public int getMaxCount() {
return maxCount;
}
public void setMaxCount(int maxCount) {
this.maxCount = maxCount;
}
}
public static class FirstChild extends AbstractChild {
@Override
public boolean isExportEnabled() {
return false;
}
@Override
public Type getType() {
return Type.TYPE_A;
}
}
public static class SecondChild extends AbstractChild {
@Override
public boolean isExportEnabled() {
return true;
}
@Override
public Type getType() {
return Type.TYPE_B;
}
}
public static class Fam {
private int famId;
private List<Parent> members;
public Fam() {
members = new ArrayList<Parent>();
}
public int getFamId() {
return famId;
}
public void setFamId(int famId) {
this.famId = famId;
}
public List<Parent> getMembers() {
return members;
}
public void setMembers(List<Parent> members) {
this.members = members;
}
public void addMember(Parent member) {
members.add(member);
}
}
public SimpleTestMain() {
}
public static void main(String[] args) {
Fam fam = new Fam();
FirstChild fc = new FirstChild();
fc.setId(1);
fc.setCode("FirstChildCode");
fc.setMinCount(1);
fc.setMaxCount(4);
fam.addMember(fc);
SecondChild sc = new SecondChild();
sc.setCode("SecondChildCode");
sc.setMinCount(131);
sc.setMaxCount(431);
fam.addMember(sc);
String test = "";
// Serialize it
ObjectMapper mapper = new ObjectMapper();
try {
test = mapper.writeValueAsString(fam);
System.out.println("Serialized bean:\n" + test);
// the output
// Serialized bean:
// {"famId":0,"members":[{"@cls":".SimpleTestMain$FirstChild","id":1,"code":"FirstChildCode","minCount":1,"maxCount":4,"type":"TYPE_A","exportEnabled":false},{"@cls":".SimpleTestMain$SecondChild","id":0,"code":"SecondChildCode","minCount":131,"maxCount":431,"type":"TYPE_B","exportEnabled":true}]}
} catch (IOException e) {
e.printStackTrace();
}
// Deserialize it
mapper = new ObjectMapper();
// mapper.enableDefaultTyping();
try {
Fam fam1 = mapper.readValue(test, Fam.class);
} catch (IOException e) {
e.printStackTrace();
}
}
}