我试图从客户端发布xml并从我的服务中使用xml。当我尝试以下代码时,我得到不支持的媒体类型异常。 我试着效仿这个例子: http://examples.javacodegeeks.com/enterprise-java/rest/jersey/xml-example-with-jersey-jaxb/ 我究竟做错了什么 ?有任何想法吗 ?
客户方:
private void request(String url,Event event) throws IOException, SAXException {
try {
Client client = Client.create();
ClientResponse response = null;
WebResource webResource = client
.resource(url);
if(event != null){
response = webResource.accept("application/xml")
.post(ClientResponse.class,event);
}else{
response = webResource.accept("application/xml")
.get(ClientResponse.class);
}
if (response.getStatus() != 200) {
throw new RuntimeException("Failed : HTTP error code : "
+ response.getStatus());
}
String output = response.getEntity(String.class);
System.out.println("Output from Server .... \n");
System.out.println(output);
}catch (Exception e) {
e.printStackTrace();
}
服务器端:
@POST
@Path("/created")
@Consumes({"application/xml", MediaType.APPLICATION_FORM_URLENCODED,"text/html"})
@Produces ({"application/xml", MediaType.APPLICATION_FORM_URLENCODED,"text/html"})
public Response createEvent( @FormParam("eventtitle") String title,
@FormParam("eventtype") String type,
@FormParam("eventdescription") String description,
@FormParam("eventlocation") String location,
@FormParam("eventdate") String date,
@FormParam("eventcreationdate") String creationdate,
@FormParam("eventmodificationdate") String modificationdate,
@Context HttpServletResponse servletResponse) throws IOException {
final StringBuilder sb = new StringBuilder();
sb.append( "<h4>Event sucessfully created </h4>");
sb.append("<br></br><li><p><a href=\"/\">go back</a></p></li>");
Response.status(200).entity(sb.toString()).build();
Event event = eventWorker.createEvent(title,type,description,location,date,
creationdate,modificationdate);
return Response.ok(sb.toString()).cacheControl(CC).build();
}