Jersey/Jaxb returns list of strings instead of integers

By : Reini
Source: Stackoverflow.com
Question!

this is the class, that my jersey service returns:

@XmlRootElement(name="chart-data")
public class ChartDataDto {
private List<Series> series = new ArrayList<>();

    public ChartDataDto()
    {

    }

    public void putSeries(String name, Integer... series)
    {
        this.series.add(new Series(name, series));
    }

    @XmlElement(name="series")
    public List<Series> getSeries()
    {
        return this.series;
    }

    @XmlRootElement(name="series")
    static class Series
    {
        @XmlElement(name="name")
        public String name;
        @XmlElement(name="values")
        public List<Integer> series;

        public Series()
        {

        }

        public Series(String name, Integer... series)
        {
            this.name = name;

            this.series = Arrays.asList(series);
        }
    }
}

The JSON string returned is for example:

{"series":[
    {
        "name":"Series 1",
        "values":["1","2","2","3","3","4","4","5","5","6","6","7","7"]
    },{
        "name":"Series 2",
        "values":["7","7","6","6","5","5","4","4","3","3","2","2","1"]
    }
]}

But I think the correct JSON string should be:

{"series":[
    {
        "name":"Series 1",
        "values":[1,2,2,3,3,4,4,5,5,6,6,7,7]
    },{
        "name":"Series 2",
        "values":[7,7,6,6,5,5,4,4,3,3,2,2,1]
    }
]}

What's wrong there? Wrong type for the integer list? Wrong annotations?

By : Reini


Answers

Using Jackson and Gson your code works well.

By : Ilya


This video can help you solving your question :)
By: admin