This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Rust Web Development and have Bastian Gruber on-line!
See this thread for details.
Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

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

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 .
 
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can use com.sun.jersey.api.client.ClientResponse


followed by
 
susant panda
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 989
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i already gave annotation like @XMLRootElement to my Students class .. still i m getting this error ..
 
E Armitage
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic