Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

JAX-RS Provider for deserializing

  • 01-09-2011 10:45am
    #1
    Registered Users, Registered Users 2 Posts: 10


    I have a question regarding the use of Providers to serialize/deserialize data when using Rest within Java. I am using an example where I POST an image to a Rest service.

    This is the Provider class. (To make it easier to read I've omitted all imports)
    @Provider
    @Consumes("image/jpeg")
    class ImageProvider implements MessageBodyReader<Image> {
    
        @Override
        public Image readFrom(Class<Image> type,
                Type genericType,
                Annotation[] annotations,
                MediaType mediaType,
                MultivaluedMap<String, String> httpHeaders,
                InputStream entityStream) throws IOException,
                WebApplicationException {
            return ImageIO.read(entityStream);
        }
    
        @Override
        public boolean isReadable(Class<?> arg0, Type arg1, Annotation[] arg2,
                MediaType arg3) {
            // TODO Auto-generated method stub
            return false;
        }
    
    }
    

    This is the Rest class. (Again, imports omitted)
    @Path ("/rsimage")
    public class RSImage {
        
        @Path("/active")
        @GET
        @Produces("text/html")
        public String activeTest(){
            return "<html lang=\"en\"><body>active</body></html>";
        }
        
        @Path("jpeg")
        @Consumes("image/jpeg")
        @Produces("text/html")
        @POST
        public String printImageDimesions(Image image){        
            int imageWidth = image.getWidth(null);
            int imageHeight = image.getHeight(null);
            return "<html lang=\"en\"><body>width: [" + imageWidth + 
            "] height: [" + imageHeight + "]</body></html>";
        }
    }
    

    I confirm the service is running by calling the /active test method.

    Can anyone tell me how I need to modify the class so that the printImageDimesions() method can access the ImageProvider class. I've been searching Google for ages and just can't find any examples of this in use.


Comments

  • Registered Users, Registered Users 2 Posts: 2,013 ✭✭✭lynchie


    Why do you need to access the provider from your service? The provider is responsible for converting the input stream to an Image which it has given you in the printImageDimensions() method.


  • Registered Users, Registered Users 2 Posts: 10 fruitcow


    lynchie wrote: »
    Why do you need to access the provider from your service? The provider is responsible for converting the input stream to an Image which it has given you in the printImageDimensions() method.

    The problem I have is that the Provider is in no way linked to the service class. It's not referenced in any configuration files (web.xml, etc) and in it's current form does nothing, it's just a standalone class. I know it doesn't work as it is right now but I don't know how to get it to work.

    What I am trying to figure out is how to get the RSImage class to make use of the ImageProvider class.


  • Registered Users, Registered Users 2 Posts: 2,013 ✭✭✭lynchie


    You have to register your provider class. How you do this depends on what you are using. I use jersey and it scans my packages for classes to the Provider annotation. Think resteasy does that too. I assume the others do it too.

    Also, your provider returns false in the isReadable, so you are saying that the provider should never be used.

    Use some debugging to prove that your providers isReadable method is being called.


  • Registered Users, Registered Users 2 Posts: 10 fruitcow


    lynchie wrote: »
    You have to register your provider class. How you do this depends on what you are using. I use jersey and it scans my packages for classes to the Provider annotation. Think resteasy does that too. I assume the others do it too.

    Also, your provider returns false in the isReadable, so you are saying that the provider should never be used.

    Use some debugging to prove that your providers isReadable method is being called.

    I'm using glassfish to test but when it starts up for some reason it doesn't automatically register the providers even though it is configured to be jersey aware. For now I've configured the provider in the web.xml and it seems to be doing the trick. I'm not looking to see whuu

    Thanks for pointing out the isReadable change. The code was automatically generated from eclipse and defaults to false.

    Now I'm getting a 415 error code which is related to the media-type not being supported. When I fix this I'll post the code including the test case if anyone is interested.


Advertisement