Interface handling in REST and JAXB

It is a very common scenario when you are writing a class implementing and interface and your parameters to the classes are interfaces.

eg:

 
public interface Person {}

@XmlRootElement
public class Employee implements Person {}

@Path("/persons")
public class MyEmployeeResources {
@PUT
@Consumes("application/xml")
public void putEmployee(Person person) {...}
}

In the above example you will get an error with Jersey or RESTEasy as they both don't know the implementation of Person are JAXB class, hence they don;t have the context of JAXB.

Really cool alternative to this was to use @XMLSeeAlso annotation. something like below

 
@XmlSeeAlso(Employee.class)
public interface Person {}

This way JAXB creates the JAXBContext of Person, because it knows that it can be unmarshal with Employee or Student class.

Similarly @XMLSeeAlso can be used with JAX-WS.

0 comments:

Post a Comment