Jersey - populate java beans from XML string

Question!

I have a Jersey client that is successfully calling a REST service and populating the associated Java Beans CustomerType based on this code:

WebResource service = client.resource(SECURE_BASE_URL);.
CustomerType cust = service.path("customer").path("23210")
    .accept(MediaType.TEXT_XML).get(CustomerType.class);

What I would like is to call the service with

 service.path("customer").path("23210").accept(MediaType.TEXT_XML).get(String.class);

to get the XML string and then convert the XML to the CustomerType bean. I would like to do it this way for logging and to help with designing the system. Is there a ways to convert the XML to the bean?

By : Haphazard


Answers

Jersey provides a logging filter. This post answers your question.



There are many ways to do that. If your CustomerType class is JAXB-annotated (@XmlRootElement or whatever), you can simply use an Unmarshaller constructed via a JAXBContext (that you have previously initialized with your packages) like this:

CustomerType customerType = (CustomerType) jaxbContext.createUnmarshaller()
        .unmarshal( new StringReader(yourString) );


This video can help you solving your question :)
By: admin