How do you marshall a parameterized type with JAX-WS / JAXB?

Posted by LES2 on Stack Overflow See other posts from Stack Overflow or by LES2
Published on 2010-02-17T03:59:00Z Indexed on 2010/05/16 2:10 UTC
Read the original article Hit count: 453

Filed under:
|
|
|
|

Consider the following classes (please assume public getter and setter methods for the private fields).

// contains a bunch of properties
public abstract class Person { private String name; }

// adds some properties specific to teachers
public class Teacher extends Person { private int salary; }

// adds some properties specific to students
public class Student extends Person { private String course; }

// adds some properties that apply to an entire group of people
public class Result<T extends Person> {
   private List<T> group;

   private String city;
   // ...
}

We might have the following web service implementation annotated as follows:

@WebService
public class PersonService {

    @WebMethod
    public Result<Teacher> getTeachers() { ... }

    @WebMethod
    public Result<Student> getStudents() { ... }
}

The problem is that JAXB appears to marshall the Result object as a Result<Person> instead of the concrete type. So the Result returned by getTeachers() is serialized as containing a List<Person> instead of List<Teacher>, and the same for getStudents(), mutatis mutandis.

Is this the expected behavior? Do I need to use @XmlSeeAlso on Person?

Thanks!

LES

© Stack Overflow or respective owner

Related posts about jax-ws

Related posts about webservices