jackson子类反序列化列表

时间:2016-04-14 08:52:44

标签: java xml jackson

我正在开发一个web服务,我应该提供xml作为响应,我正在使用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;
    }

}

以上生成输出xml

<ClientObject>
    <accounts>
        <MySubItemA>
            ....
        </MySubItemA>
    </accounts>
    <accounts>
        <MySubItemB>
            ....
        </MySubItemB>
    </accounts>
</ClientObject>

有没有办法删除<accounts>包装器,即使我使用的是@JacksonXmlElementWrapper(useWrapping = false),如果我删除了这个注释,它会执行以下操作

<ClientObject>
    <accounts>
        <accounts>
            <MySubItemA>
                ....
            </MySubItemA>
        </accounts>
        <accounts>
            <MySubItemB>
                .....
            </MySubItemB>
        </accounts>
    </accounts>
</ClientObject>

所以最后的输出应该是

<ClientObject>
        <MySubItemA>
            ....
        </MySubItemA> 
        <MySubItemB>
            ....
        </MySubItemB> 
</ClientObject>

感谢您的帮助。

祝你好运 Sajid

1 个答案:

答案 0 :(得分:0)

我能够弄明白,所以我认为它可以帮助别人:

我创建了Serializer

package com.spinner.jackson;

import java.io.IOException;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

public class MySerial extends JsonSerializer<ClientObject>
{
   public void serialize(ClientObject value, JsonGenerator jgen,
SerializerProvider provider)
       throws IOException, JsonProcessingException
   {
       jgen.writeObjectFieldStart(value.getClass().getSimpleName()); 
       jgen.writeObjectField(value.getAccounts().get(0).getClass().getSimpleName(), value.getAccounts().get(0)); 
       jgen.writeObjectField(value.getAccounts().get(1).getClass().getSimpleName(), value.getAccounts().get(1)); 
       jgen.writeObjectField(value.getAccounts().get(2).getClass().getSimpleName(), value.getAccounts().get(2)); 
       jgen.writeObjectField(value.getAccounts().get(3).getClass().getSimpleName(), value.getAccounts().get(3)); 

   }

}

然后我在ClientObject.java中执行了以下操作:

package com.spinner.jackson;

import java.util.ArrayList;
import java.util.List;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;

@JsonSerialize(using = CustomDeserial.class)
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>();
    }

    public void setL(List<MyItem> l) {
        this.accounts = l;
    }

    public List<MyItem> getAccounts() {
        return this.accounts;
    }
}

并且它有效,希望这对某人有所帮助。