1
私は応答としてxmlを提供するはずのWebサービスに取り組んでいます。私はそのためにjacksonを使用しています。jacksonデシリアライゼーションのサブクラスのリスト
package com.spinner.jackson;
import java.util.ArrayList;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonUnwrapped;
import com.fasterxml.jackson.annotation.JsonValue;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlText;
public class ClientObject {
@JacksonXmlElementWrapper(useWrapping = false)
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.WRAPPER_OBJECT)
private List<MyItem> accounts;
public ClientObject(List<MyItem> pl) {
this.accounts = pl;
}
public ClientObject() {
this.accounts = new ArrayList<MyItem>();
}
@JsonDeserialize(using = CustomDeserial.class)
public void setL(List<MyItem> l) {
this.accounts = l;
}
public List<MyItem> getAccounts() {
// TODO Auto-generated method stub
return this.accounts;
}
}
、その後、私は次のようにサブのクラスがあります:私は、私は抽象クラスを持って、問題で立ち往生しています
パッケージcom.spinner.jacksonを。
import javax.xml.bind.annotation.XmlRootElement;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
@JacksonXmlRootElement(localName="itemA")
public class MySubItemA extends MyItem {
public MySubItemA() {
super();
// TODO Auto-generated constructor stub
}
public MySubItemA(int id, String name) {
super(id, name);
// TODO Auto-generated constructor stub
}
private String itemAProperty1;
private String itemAProperty2;
public String getItemAProperty1() {
return this.itemAProperty1;
}
public void setItemAProperty1(String itemAProperty1) {
this.itemAProperty1 = itemAProperty1;
}
public String getItemAProperty2() {
return this.itemAProperty2;
}
public void setItemAProperty2(String itemAProperty2) {
this.itemAProperty2 = itemAProperty2;
}
@JsonCreator
public MySubItemA(@JsonProperty("id")int id, @JsonProperty("name")String name, @JsonProperty("itemAProperty1")String p1, @JsonProperty("itemAProperty2")String p2) {
super(id, name);
this.itemAProperty1 = p1;
this.itemAProperty2 = p2;
}
}
別のサブクラス
package com.spinner.jackson;
import javax.xml.bind.annotation.XmlRootElement;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
@JacksonXmlRootElement(localName="itemB")
public class MySubItemB extends MyItem {
private int itemBProperty1;
private String itemBProperty2;
public int getItemBProperty1() {
return this.itemBProperty1;
}
public void setItemBProperty1(int itemBProperty1) {
this.itemBProperty1 = itemBProperty1;
}
public String getItemBProperty2() {
return this.itemBProperty2;
}
public void setItemBProperty2(String itemBProperty2) {
this.itemBProperty2 = itemBProperty2;
}
public MySubItemB(@JsonProperty("id")int id, @JsonProperty("name")String name, @JsonProperty("itemBProperty1")int p1, @JsonProperty("itemBProperty2")String p2) {
super(id, name);
this.itemBProperty1 = p1;
this.itemBProperty2 = p2;
}
}
とクライアントクラスに続くとおり
package com.spinner.jackson;
import java.util.ArrayList;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonUnwrapped;
import com.fasterxml.jackson.annotation.JsonValue;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlText;
public class ClientObject {
@JacksonXmlElementWrapper(useWrapping = false)
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.WRAPPER_OBJECT)
private List<MyItem> accounts;
public ClientObject(List<MyItem> pl) {
this.accounts = pl;
}
public ClientObject() {
this.accounts = new ArrayList<MyItem>();
}
@JsonDeserialize(using = CustomDeserial.class)
public void setL(List<MyItem> l) {
this.accounts = l;
}
@JsonDeserialize(using = CustomDeserial.class)
public List<MyItem> getAccounts() {
// TODO Auto-generated method stub
return this.accounts;
}
}
MYITEMクラス:上記
package com.spinner.jackson;
import javax.xml.bind.annotation.XmlRootElement;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonSubTypes.Type;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonTypeInfo.Id;
@XmlRootElement
public abstract class MyItem {
public MyItem() {
super();
// TODO Auto-generated constructor stub
}
private int id;
private String name;
public int getId() {
return this.id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public MyItem(int id, String name) {
this.id = id;
this.name = name;
}
}
続い
0123として出力XMLを生成します<ClientObject>
<accounts>
<MySubItemA>
....
</MySubItemA>
</accounts>
<accounts>
<MySubItemB>
....
</MySubItemB>
</accounts>
</ClientObject>
<ClientObject>
<accounts>
<accounts>
<MySubItemA>
....
</MySubItemA>
</accounts>
<accounts>
<MySubItemB>
.....
</MySubItemB>
</accounts>
</accounts>
</ClientObject>
ので、最終的な出力があるべき
を以下のように私はそれが何かをこの注釈を削除する場合にも、私は@JacksonXmlElementWrapper(useWrapping = false)を使用していていても<accounts>
ラッパーを削除する方法があります
<ClientObject>
<MySubItemA>
....
</MySubItemA>
<MySubItemB>
....
</MySubItemB>
</ClientObject>
ご協力いただきありがとうございます。
敬具 Sajid
抽象クラスMyItemが見つかりませんでした。 – dambros
ありがとう、更新された質問 – Sajid