how to support Polymorphism with RESTeasy Client proxy?

365 Views Asked by At

suppose this JAX-RS method :

@GET
@Path("/{id}")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
public Employee get(@PathParam("id") Long id) {
     return myService.findbyId(id);
}  

With the following POJO:

@XmlRootElement
public class Employee {
    Integer id;
    String name;  (getters;setters etc...)
}
@XmlRootElement(name="employee") 
public class SepcialEmployee extends Employee  {
     Skill skill;  (getters;setters etc...)
}
@XmlRootElement(name="employee")
public class Manager extends Employee {
    String headOffice;   (getters;setters etc...)
}  

This works fine with RESTeasy/spring-MVC integration. And if I call the method from a web browser; I can get the following answer for i.e.:

<employee Id="17">
    <name>Marc</name>
    <headOffice>accounting</headOffice>
</employee>  

But if I use the RESTeasy Client Framework for my unit test. the client proxy generated unmarsalles only the Employee Parent class and I loose the child informations (Manager.headOffice or SepcialEmployee.Skill). Below an extract of my Junit test:

public class Test {

 @Path("empl")
 public interface EmpProxy {

     @GET
     @Produces(MediaType.APPLICATION_XML)
     Employee getEmployee(@ClientURI String uri);
 }

 private static TJWSEmbeddedSpringMVCServer server;
 public static final String host = "http://localhost:8080/";
 public static final int port = 8080;
 private static EmpProxy proxy;

 @BeforeClass
 public static void setup() {
     server = new TJWSEmbeddedSpringMVCServer("classpath:test-dispatcher-servlet.xml", port);
     server.start();
     RegisterBuiltin.register(ResteasyProviderFactory.getInstance());
     ResteasyClient client = new ResteasyClientBuilder().build();
     ResteasyWebTarget target = client.target(host);
     proxy = target.proxy(EmpProxy.class);  
 }

 @Test
 public void test(){
    String url = host+"/empl/17";
    Employee employee = proxy.getEmployee(url);
    System.out.println(employee);
 }
}
0

There are 0 best solutions below