我正在尝试在REST web服务中执行向上转换。但我在响应中收到了400个错误请求。如何在web服务中执行向上转换。
@XmlRootElement
Class Animal{
String type;
...
}
@XmlRootElement
Class Tiger extends Animal{
String name;
...
}
SERVER SIDE:
===========
@POST
@Path("/upcasting")
@Produces(MediaType.APPLICATION_XML)
@Consumes(MediaType.MULTIPART_FORM_DATA)
public void testUpcasting(@FormDataParam("an") Animal anml){
System.out.println("inside testupcasting");
System.out.println(anml.getClass());
}
CLIENT SIDE:
===========
String uri = "http://localhost:8080/app/upcasting";
ClientConfig config = new DefaultClientConfig();
Client client = Client.create(config);
WebResource resource = client.resource(uri);
Tiger t = new Tiger();
t.setType("Non-veg");
t.setName("myTiger");
FormDataMultiPart multipart = new FormDataMultiPart().field("an", t, MediaType.APPLICATION_XML_TYPE);
ClientResponse response = resource.type(MediaType.MULTIPART_FORM_DATA).post(ClientResponse.class, multipart);发布于 2015-04-21 03:11:05
服务器:
@POST
@Path("/upcasting")
@Produces(MediaType.APPLICATION_XML)
@Consumes(MediaType.MULTIPART_FORM_DATA)
public void testUpcasting(Animal anml){
System.out.println("inside testupcasting");
System.out.println(anml.getClass());
}客户端:
String URL = "http://......./rest/services
Client client = ClientBuilder.newClient();
target = client.target(URL);
ObjectMapper mapper = new ObjectMapper();
Response response = target.path("/upcasting").request(MediaType.APPLICATION_XML).post(Entity.entity(mapper.writeValueAsString(anml), MediaType.APPLICATION_XML);https://stackoverflow.com/questions/29755902
复制相似问题