0

I need to access a thirdparty REST Webservice via my java class which gives the response in xml format.

How can i write a sample java class to consume this thirdparty REST service?

After googling for several days, I came to an observation that i can consume webservice using Jersey. But still am not sure how can i use Jersey in my scenario as I just need to read the response from a thirdyparty webservice. Please help.

EDIT: I tried using the website http://pojo.sodhanalibrary.com/ to convert the xml response obtained by the webservice to POJO classes. But still am not sure what exactly I need to do to proceed further.

active_coder
  • 79
  • 1
  • 2
  • 13
  • 1
    We don't know what you mean by "consume" the output from the service. Do you need to deserialize it to a POJO? Just parse the XML? The question is not really clear at all. – Jim Garrison Aug 22 '16 at 18:57
  • Again i could see a -1 for this question. I dont understand the reason behind this. Anyway I believe 'consume' is a generic keyword used for webservices and thatz the reason why i used the term here as well. My requirement is that I need to fire a request to a thirdparty rest webservice which gives output in an xml format. My question was how can i parse this response without writing any specific xml parser or something. I believe Jersey with JAXB will help here, but am not sure how can i use this. Most of the questions like this is still unanswered in stackoverflow. – active_coder Aug 23 '16 at 02:11
  • 1
    That's the problem. "Consume" is too generic and we don't know what your ultimate goal is for the returned XML. – Jim Garrison Aug 23 '16 at 03:11
  • @JimGarrison I need to read the xml response and convert it to Java objects.(I assume i have explained this in the question as well) – active_coder Aug 23 '16 at 06:03

1 Answers1

2

You need REST client for Java. There are several ways to implement it, more details can be found at:

Execute request, get response and parse it to your data structure. Jersey client example:

Client client = Client.create();

WebResource webResource = client.resource("http://localhost:8080/example/rest/service");

ClientResponse clientResponse = webResource.accept("application/xml").post(ClientResponse.class, yourRequestObject);

YourResponseType yourResponse = clientResponse.getEntity(YourResponseType.class);
Community
  • 1
  • 1
Justinas Jakavonis
  • 8,220
  • 10
  • 69
  • 114
  • Thank you @Justas the guidance. Eventhough am too late to answer this (itz almost 22 months i have posted this) I was able to achieve my requirement by using the following steps: – active_coder Jul 11 '18 at 15:54