How does biding happens in JAX-Rs xml to object?

185 Views Asked by At

I have been exploring on how to consume the web services and I found a good articles on JAX-Rs and it is the way to consume Rest Webservices. my task is to hit the URL which returns the XML as response and should be converted into Object which I have achieved using following code.

 client = ClientBuilder.newClient();
 //example query params: ?q=Turku&cnt=10&mode=json&units=metric
  target = client.target(
                "http://api.openweathermap.org/data/2.5/weather")
                   .queryParam("appid", apikey)
                   .queryParam("units", "metric")
                ;

And here is the piece of code which will map my XML response to java object

  Example exampleObject =  target.request(MediaType.APPLICATION_XML).get(Example.class);

This works fine, but the issue is my lead is saying use JIBX since it's faster.

Question 1 : How does target.request converts the xml response (does it use jibx or jaxb etc ?? ) Question 2 : If I have use JIBX I need to download the response as stream and do the marshalling and unmarshalling which I found not a right way to consume webservices, I am I right??

Please do help on this.

1

There are 1 best solutions below

0
Andy McCright On

1: JAX-RS uses MessageBodyReaders to convert the HTTP entity stream into an object. By default, all JAX-RS implementations are required to ship a MessageBodyReader (and Writer) that uses JAXB to serialize/deserialize to/from XML when the content type is application/xml.

2: If you want to use something other than JAXB to deserialize XML, you could write your own MessageBodyReader that consumes “application/xml”. That would override the built-in JAXB reader. An example of how to do this is available here: https://memorynotfound.com/jax-rs-messagebodyreader/

Hope this helps, Andy