假设这个 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 Client Framework 进行单元测试。客户端代理生成的仅解组 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);
}
}