Reputation: 92
I want to consume a webservice returning XML data by
Client client = ClientBuilder.newClient();
String seLogerAPI = "http://ws.seloger.com/search.xml";
// Defining some query params. Full doc : https://github.com/bodinsamuel/seloger-php/blob/master/API.md
// More : https://github.com/pasnox/housing/blob/master/SeLoger.com.api.txt
WebTarget target = client.target(seLogerAPI)
.queryParam("idtypebien", "1") // apartements
.queryParam("idtt", "1") // Renting
.queryParam("nb_pieces", "5")
.queryParam("cp", "95330")
;
Response result = target.request(MediaType.TEXT_XML_TYPE).get();
ResponseRecherche res = result.readEntity(new GenericType<ResponseRecherche>() {});
System.out.println(res);
However i'm facing this error : MessageBodyReader not found for media type=text/xml;charset=utf-8
Response result = target.request(MediaType.TEXT_XML_TYPE).get();
is working fine but when i try to put it in POJO by using
ResponseRecherche res = result.readEntity(new GenericType<ResponseRecherche>() {});
the org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException is raised...
FYI : ResponseRecherche is a POJO that has the same structure than the answer from the webservice. I was following this tutorial : https://vaadin.com/blog/-/blogs/consuming-rest-services-from-java-applications
I don't really understand why
My pom.xml
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.25</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-jaxb</artifactId>
<version>2.25</version>
</dependency>
Thanks for reading
Upvotes: 3
Views: 2412
Reputation: 1283
The error message is indicating that your JAX-RS implementation is unable convert the XML into your POJO (ResponseRecherche). JAX-RS uses MessageBodyReaders to convert response entities into Java objects. All JAX-RS implementations are required to provide a set of built-in MessageBodyReaders, but users may provide their own for custom conversion.
You likely have two solutions: 1) Use the built-in JAXB MessageBodyReader - to do that you will need to annotate your POJO with JAXB annotations such as @XmlRootElement, etc. A good tutorial for using JAXB with JAX-RS - including how to annotate your object classes can be found here: https://dennis-xlc.gitbooks.io/restful-java-with-jax-rs-2-0-2rd-edition/en/part1/chapter6/jaxb.html
2) Create and register your own MessageBodyReader that can convert the XML from the remote site to your ResponseRecherche object. The same tutorial has some good information on how to create custom MessageBodyReaders here: https://dennis-xlc.gitbooks.io/restful-java-with-jax-rs-2-0-2rd-edition/en/part1/chapter6/custom_marshalling.html
Note that since you are using the JAX-RS Client APIs, you will need to explicitly register your custom MessageBodyReader like so:
Client client = ClientBuilder.newClient();
client.register( MyMessageBodyReader.class );
Hope this helps, Andy
Upvotes: 3