How to maintain polymorphism with RESTeasy proxy?

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 great with RESTeasy / spring-MVC integration. And if I call the method from the web browser; I can get the following response, for example:

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

      

But if I use RESTeasy Client Framework for my unit test. client proxy generates unmarsalles only Employee Parent class and I am losing information about it (Manager.headOffice or SepcialEmployee.Skill) . Below is an excerpt from 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);
 }
}

      

+3


source to share





All Articles