2

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?

Haphazard
  • 10,900
  • 6
  • 43
  • 55

2 Answers2

4

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) );
Costi Ciudatu
  • 37,042
  • 7
  • 56
  • 92
  • Quite right, I had actually found that method of doing it. I was hoping for a native Jersey way to do it but I suppose it doesn't matter enough to worry about it. – Haphazard Jun 17 '11 at 18:13
0

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

Community
  • 1
  • 1
yves amsellem
  • 7,106
  • 5
  • 44
  • 68
  • Indeed, and if you look at my question, I mentioned that I was able to go directly to the `CustomerType` object. I was looking for a slick way to convert the returned XML to the `CustomerType` by going through Jersey but without making a second call to the server. – Haphazard Jun 22 '11 at 11:47