0

我有一个具有泛型类型属性的基类泛型和几个从它继承的类。像这样的东西:

public abstract class BaseClass<T extends Number> { 
    @XmlAnyElement
    public T getId() { return id; }
    private T id ; // init for hibernate bug workaround 
.....
}

@XmlRootElement
public class A extends BaseClass<Integer> {
    private String name;
    private String error;
    private String url; 

    public String getError() { return error; }  
    public void setError(String error) { this.error = error; }

    public String getName() { return name; }
    public void setName(String name) { this.name = name; }

    public void setUrl(String url) { this.url = url; }
    public String getUrl() { return url; }
}

当泽西试图编组时,它会抛出:

    javax.xml.bind.MarshalException
    - with linked exception:
   [com.sun.istack.SAXException2: unable to marshal type "java.lang.Integer" as an        element because it is missing an @XmlRootElement annotation]
at com.sun.xml.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:318)
at com.sun.xml.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:172)
at com.sun.jersey.json.impl.JSONMarshallerImpl.marshal(JSONMarshallerImpl.java:94)
at com.sun.jersey.json.impl.BaseJSONMarshaller.marshallToJSON(BaseJSONMarshaller.java:103)
at com.sun.jersey.json.impl.BaseJSONMarshaller.marshallToJSON(BaseJSONMarshaller.java:91)
    ......

我无法将 XmlRootElement 注释添加到 Integer,那我该怎么办?

4

1 回答 1

0

您应该尝试将@XmlRootElement注释添加到您的BaseClass.

于 2012-07-02T12:10:04.207 回答