Unmarshalling generic list with JAXB

社会主义新天地 提交于 2019-11-28 21:52:28
Fedy2

Thanks to Blaise Doughan and his article I've found the solution.

First we need the Wrapper class provided in the article:

@XmlRootElement
public class Wrapper<T> {

  private List<T> items;

  public Wrapper() {
    items = new ArrayList<T>();
  }

  public Wrapper(List<T> items) {
    this.items = items;
  }

  @XmlAnyElement(lax=true)
  public List<T> getItems() {
    return items;
  }
}

Then I've modified the Response class in order to use it:

@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Response<T> {

  @XmlElement
  protected String status;

  @XmlElement
  protected Wrapper<T> result;

  ...

  public Response(String status, List<T> result) {
    this.status = status;
    this.result = new Wrapper<>(result);
  }

  ...

  public List<T> getResult() {
    return result.getItems();
  }

  ...
}

Finally the unmarshalling code:

JAXBContext context = JAXBContext.newInstance(Response.class, Project.class, User.class, Wrapper.class);
Unmarshaller unmarshaller = context.createUnmarshaller();

StreamSource source = new StreamSource(new File("responseProject.xml"));
Response<Project> responseProject = (Response<Project>)unmarshaller.unmarshal(source);
System.out.println(responseProject.getStatus());
for (Project project:responseProject.getResult()) System.out.println(project);

source = new StreamSource(new File("responseUser.xml"));
Response<User> responseUser = (Response<User>)unmarshaller.unmarshal(source);
System.out.println(responseUser.getStatus());
for (User user:responseUser.getResult()) System.out.println(user);

I've added the Wrapper class to the context class list.

Alternatively you can add this annotation to the Response class:

@XmlSeeAlso({Project.class, User.class})

Using @XmlSeeAlso({Project.class, User.class}) on Response classes has the drawback of generating some garbage information on each entity in the list: xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="userAccount"

<resources>
    <links>
        <link>
            <rel>self</rel>
            <uri>http://localhost:8080/salonea-1.0/rest/user-accounts?offset=0&amp;limit=2</uri>
        </link>
        <link>
            <rel>prev</rel>
            <uri></uri>
        </link>
        <link>
            <rel>next</rel>
            <uri>http://localhost:8080/salonea-1.0/rest/user-accounts?offset=2&amp;limit=2</uri>
        </link>
    </links>
    <collection>
        <user-account 
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="userAccount">
            <accountType>user</accountType>
            <activationCode>638f502a0e409348ccc2e36c24907f0</activationCode>
            <email>michzio@hotmail.com</email>
            <login>michzio</login>
            <password>sAmPL3#e</password>
            <registrationDate>2015-09-03T17:30:03+02:00</registrationDate>
            <userId>1</userId>
        </user-account>
        <user-account 
            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="userAccount">
            <accountType>user</accountType>
            <activationCode>334bc79d142a291894bd71881e38a719</activationCode>
            <email>alicja@krainaczarow.com</email>
            <login>alicja</login>
            <password>zAczka!00</password>
            <registrationDate>2015-09-03T17:30:03+02:00</registrationDate>
            <userId>2</userId>
        </user-account>
    </collection>
</resources>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!