0

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?

4

1 回答 1

0

我正在谈论/大声询问的两种方法......都有效,一种是黑客,另一种是我可以忍受的东西。两者都利用泽西岛来完成这项工作:

方法A(更好): https ://gist.github.com/pulkitsinghal/5779383#file-approach_a-java

方法B(hack~ish ...可能性能较差): https ://gist.github.com/pulkitsinghal/5779383#file-approach_b-java

于 2013-06-14T04:08:02.190 回答