Convert list to list using JAXB

I am trying to annotate a set of data objects using JAXB binding annotations so that this set of data objects can be correctly marshaled as JSON using CXF. I ran into a problem with a class, which basically is a wrapper around an ArrayList:

class IntegerListWrapper {
    private ArrayList<Integer> integerList;
    ...
}

Some of my data objects belong to this class:

class DataObjectFoo {
    ...
    public IntegerListWrapper getDataIDs() {
        ...
    }
    ...
}

I am looking for a way out:

"DataObjectFoo" : {
  "dataIDs" : [1, 2, ..., n] // Array of Data IDs
}

I tried annotating the IDList class, but it left me with this:

"DataObjectFoo" : {
  "dataIDs" : { "integerList" : [1, 2, ..., n] } // Extra nesting
}

I tried writing an XmlAdapter, but got mixed results:

// Throws an error... "Can't bind to interface"
public final class IDListAdapter extends XmlAdapter<List<Integer>, IDList> {
// Does not produce any output
public final class IDListAdapter extends XmlAdapter<ArrayList<Integer>, IDList> {
// Produces output with extra nesting like above
public final class IDListAdapter extends XmlAdapter<Integer[]>, IDList>

I have two questions:

  • How to get the desired result (without converting IDList to something else in my data objects)?
  • Why didn't the second XmlAdapter (using ArrayList) produce a result?
+3
1

, . "getDataIds()" ?

0

Source: https://habr.com/ru/post/1779299/


All Articles