How to convert web service response input stream to java object?
Upon executing a http request on my web serivce, I get a javax.ws.rs.core.Response
object. When I call the getEntity
method on the Response
object I get an input stream, which is a string representation of JSON. Does anyone know how I can/should convert this string into a java object? Since, I'm us开发者_运维知识库ing CXF can I use the JAXBContext? Or must I create a custom message body reader? Thanks.
Jackson has built-in JAX-RS support.
See org.codehaus.jackson.jaxrs.JacksonJsonProvider
and org.codehaus.jackson.jaxrs.JacksonJaxbJsonProvider
This is an example. It will help someone some day. (This is using a sample XML Input)
@POST
@Path("/unmarshall")
@Produces(MediaType.APPLICATION_XML)
@Consumes(MediaType.APPLICATION_XML)
public Response unmarshall(InputStream inputStream) throws JAXBException {
JAXBContext jaxbContext = JAXBContext.newInstance(MyJaxbClass.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
Object unmarshal = unmarshaller.unmarshal(inputStream);
return Response.ok().entity(unmarshal).build();
}
精彩评论