How to get object instead of XML from Jeresy rest client
I have written a jersey Restful WS and deployed it onto Tomcat.
public class MyController {
@GET
@Produces(MediaType.APPLICATION_XML)
@Path("/getMetricsByClientId/{clientId}")
public BatchProgressMetricsListRoot getAllBatchProgressMetricsForClient(@PathParam("clientId") String clientId) {
//dosomething
}
While on the client side i am calling
webResource = client.resource(metricsCaptureUrl);
batchProgressMetrics = webResource.get(BatchProgressMetricsListRoot.class);
System.out.println(batchProgressMetrics);
Now i get the object but with default values. When i try it from browser i get the xml with proper value. Now if i give
webResource = client.resource(metricsCaptureUrl);
batchProgressMetrics = webResource.get(String.class);
System.out.println(batch开发者_开发百科ProgressMetrics);
It prints proper xml. My question is is there a way i can get the object back instead of as a string? If not then can you suggest me some xml framework which i can use to generate the object manually?
Thanks, Ajay
My question is is there a way i can get the object back instead of as a string?
No. When you annotate your web service with:
@Produce(MediaType.APPLICATION_XML)
You are effectively specifying the content of your response, that is XML. REST services use many existing features of the HTTP protocol therefore you just can't return Java Objects - as in Corba or EJB.
If you want to de-serialize your objects from XML to Java in your client code, you can choose among many frameworks: JAXB, XStream are the most popular. It is recommended that you use the same serialization framework on both the client and the server.
精彩评论