[英]How to perform upcasting in web services REST jersey?
我正在尝试在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);
服务器:
@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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.