I have annotated my POJOs in Jersey and I have test methods like this one:
@POST
@Path("/test/pojo/transformation")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_XML)
public MyPojo testSerializationForLineItemRequestPojo(MyPojo myPojo)
{
return myPojo;
}
which accept the JSON flavor of the POJO via a webrequest and return the XML version of the same pojo. Usually I do this to figure out if I have all my objects annotated properly for going back and forth between their expected xml and json formats.
But now I need to perform such a transformation locally inside one of my webservice methods.
Q) How can I leverage the object transformation infrastructure seen above?
I don't want to resort to a hack ... like making a webservice call to localhost and go thru a method which is exposed so that it can transform and return the object to me.
Update#1
One idea that comes to mind is a method like:
@Produces(MediaType.APPLICATION_JSON)
private MyPojo convert(String data)
{
new InBoundHeaders().add("Content-Type", MediaType.APPLICATION_XML);
ClientResponse cr = new ClientResponse(
Response.Status.OK,
new InBoundHeaders().add("Content-Type", MediaType.APPLICATION_XML),
null,
messageBodyWorkers_singleton);
return cr.getEntity(MyPojo.class);
}
where a fake ClientResponse
can be used to quickly tap into the existing jersey infrastructure and convert xml to POJO and then again to json ... and the method wouldn't need to be exposed as a webservice for this. But I'm not sure how exactly I can tap into Jersey's pre-existing/pre-configured for MessageBodyWorkers
implementation (i call it messageBodyWorkers_singleton just to get my point across) that came for free when the jersey server started. Any ideas on how to auto wire a MessageBodyWorkers
reference?