JAX-RS MessageBodyReader

Tags: ,



I’m learning how the MessageBodyReader method works from the providers. I see the method returns an object and I’m not sure how to access the object from a service. Could I get an explanation on how to get the object returned from the reader class? This would help me apply a reading rule for all DTOs. Thanks in advance!

Service:

    @POST
    @Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
    @Path("/CreateAccount")
    @Consumes({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
    public Response createAccount(@Context HttpServletRequest req) {
        
        String a = "Reader success? ";//Would to see that string here!
        return Response.ok().build();
    }

Provider:

@Provider
public class readerClass implements MessageBodyReader<Object>
{

@Override
public boolean isReadable(Class<?> paramClass, Type paramType,
        Annotation[] paramArrayOfAnnotation, MediaType paramMediaType) {
    // TODO Auto-generated method stub
    return true;
}

@Override
public Object readFrom(Class<Object> paramClass, Type paramType,
        Annotation[] paramArrayOfAnnotation, MediaType paramMediaType,
        MultivaluedMap<String, String> paramMultivaluedMap,
        InputStream paramInputStream) throws IOException,
        WebApplicationException {
    // TODO Auto-generated method stub
    
    return "Successfully read from a providers reader method";
}

}

Answer

You misunderstood the purpose MessageBodyReader , it is used for the following purpose :

Contract for a provider that supports the conversion of a stream to a Java type. To add a MessageBodyReader implementation, annotate the implementation class with @Provider. A MessageBodyReader implementation may be annotated with Consumes to restrict the media types for which it will be considered suitable

Example : If you have a use case where you getting come custom format other than xml/json ,you want to provide your own UnMarshaller you can use messagebody reader

    @Provider
    @Consumes("customformat")
    public class CustomUnmarshaller implements MessageBodyReader {

        @Override
        public boolean isReadable(Class aClass, Type type, Annotation[] annotations, MediaType mediaType) {
            return true;
        }


        @Override
        public Object readFrom(Class aClass, Type type, Annotation[] annotations, MediaType mediaType, MultivaluedMap multivaluedMap, InputStream inputStream) throws IOException, WebApplicationException {
            Object result = null;
            try {
                result = unmarshall(inputStream, aClass); // un marshall custom format to java object here
            } catch (Exception e) {
                e.printStackTrace();
            }

            return result;


}
}

In webservice you can use this like ..

    @POST    
    @Path("/CreateAccount")
    @Consumes("custom format")
    public Response createAccount(@Context HttpServletRequest req,Account acc) {

        saveAccount(acc); // here acc object is returned from your custom unmarshaller 
        return Response.ok().build();
    }

More Info : Custom Marshalling/UnMarshalling Example , Jersy Entity Providers Tutorial



Source: stackoverflow