在Java中将通用对象转换为XML

时间:2016-06-27 14:44:44

标签: java xml generics

以下代码正常运行:

import java.io.Serializable;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "data")
public class Data implements Serializable{

    private static final long serialVersionUID = 1L;
    private long value1;

    public Data(){}
    public Data(long v1){
        this.value1 = v1;
    }
    public long getValue1() {
        return value1;
    }
    @XmlElement
    public void setValue1(long value1) {
        this.value1 = value1;
    }
}

然而,这并不是:

@XmlRootElement(name = "data")
public class Data implements Serializable{

    private static final long serialVersionUID = 1L;
    private Object value1;

    public Data(){}
    public Data(Object v1){
        this.value1 = v1;
    }
    public Object getValue1() {
        return value1;
    }
    @XmlElement
    public void setValue1(Object value1) {
        this.value1 = value1;
    }
}

正如您所看到的,我改变的是原始long的原始Object数据类型。

这会使服务器崩溃,似乎XML库无法处理通用对象。

是否有可能告诉XML库将每个对象视为运行时的任何类?

0 个答案:

没有答案