如何使用RESTeasy客户端代理支持多态性?

时间:2015-05-25 15:45:29

标签: spring-mvc jaxb jax-rs resteasy tjws

假设这个JAX-RS方法:

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

使用以下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...)
}  

这适用于RESTeasy / spring-MVC集成。如果我从网络浏览器调用该方法;我可以得到以下答案:

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

但是,如果我使用RESTeasy客户端框架进行单元测试。 客户端代理只生成了unmarsalles Employee Parent类,并且我放弃了子信息(Manager.headOffice或SepcialEmployee.Skill)。以下是我的Junit测试的摘录:

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 个答案:

没有答案