0

The following code works correctly:

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;
    }
}

However this doesn't:

@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;
    }
}

As you can see all I changed was the primitive long data type to the generic Object.

This crashes the server, it seems the XML library can't deal with generic objects.

Is there maybe a way to tell the XML library to treat each object as whatever class it is during runtime?

Greg Peckory
  • 375
  • 1
  • 7
  • 17
  • 1
    What exactly are you trying to achieve? Is it for web service or writing to a file? Which library are you using? – MojoJojo Jun 27 '16 at 14:48
  • Web service using jersey Framework. See edit for libraries – Greg Peckory Jun 27 '16 at 14:50
  • Consider an xml root element which has a list of inner elements. This could be a list of numbers, or a list of other xml objects. Hence the generic object. This is to save me having to write a class for each specific situation – Greg Peckory Jun 27 '16 at 14:52
  • Does this help: http://stackoverflow.com/questions/7037531/fetching-rest-resource-as-listt-with-jersey ? – MojoJojo Jun 27 '16 at 14:57

0 Answers0