my dog learned polymorphism*
The moose likes Web Services and the fly likes trying to consume a xml and produce the xml again using JERSY Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Web Services
Bookmark "trying to consume a xml and produce the xml again using JERSY " Watch "trying to consume a xml and produce the xml again using JERSY " New topic
Author

trying to consume a xml and produce the xml again using JERSY

susant panda
Greenhorn

Joined: Jun 04, 2012
Posts: 4
hi ,

i was trying to post an xml using jersy. here is the code

server side :

@POST
@Consumes(MediaType.APPLICATION_XML)
@Produces(MediaType.APPLICATION_XML)
@Path("consumeXML")
public Students getStudent(Students students){

System.out.println(students.getName());

students.setName(students.getName() + "abc");
students.setAge(students.getAge() + "20");
students.setSex("M");
return students;

}

client side :

Client client = Client.create();
String studentXML = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><students><age>24</age><name>AAAbcbcb</name><sex>M</sex></students>";
WebResource webResource = client
.resource("http://192.168.25.62:8080/de.vogella.jersey.first/rest/hello/consumeXML");
webResource.type(MediaType.APPLICATION_XML).post(studentXML);


when i run the client side program it posts the "studentXML" to the above "getStudent()" method in the server side and bind the xml content to the student object . I modified the student object now and i am returning the object again .

Now i want to display the returned object in XML format again , but i dont knw how to do it in the client side .. Please help me if any body knows it .
E Armitage
Rancher

Joined: Mar 17, 2012
Posts: 892
    
    9
You can use com.sun.jersey.api.client.ClientResponse


followed by
susant panda
Greenhorn

Joined: Jun 04, 2012
Posts: 4
hi Armitage,

thanks for your reply ... i tried by writing the following code

ClientResponse response = webResource.accept(MediaType.APPLICATION_XML).get(ClientResponse.class);
Students entity = response.getEntity(Students.class);
System.out.println(entity);

its throwing the following error now as below



Exception in thread "main" com.sun.jersey.api.client.ClientHandlerException: A message body reader for Java class de.vogella.jersey.first.pkg.Students, and Java type class de.vogella.jersey.first.pkg.Students, and MIME media type text/html; charset=utf-8 was not found
E Armitage
Rancher

Joined: Mar 17, 2012
Posts: 892
    
    9
your class de.vogella.jersey.first.pkg.Students needs to be autoconvertible to one of your "Produces types in this case XML so make it an XMLRootElement
susant panda
Greenhorn

Joined: Jun 04, 2012
Posts: 4
i already gave annotation like @XMLRootElement to my Students class .. still i m getting this error ..
E Armitage
Rancher

Joined: Mar 17, 2012
Posts: 892
    
    9
Your error message has
and MIME media type text/html;
which doesn't look right. Have you forgotten to set the type somewhere? Also looks like you are now doing a GET?
Maybe start with this example and change it bit by bit to suit your classes: http://docs.oracle.com/javaee/6/tutorial/doc/gkoib.html
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: trying to consume a xml and produce the xml again using JERSY
 
Similar Threads
HttpClient and MultipartEntity vs. Jersey Multipart
what is wrong with this xml code
uploading data into JTable
uploading data into JTable
XML block extraction