users@jersey.java.net

[Jersey] Example of consuming raw binary content?

From: Casper Bang <casper.bang_at_gmail.com>
Date: Tue, 29 Mar 2011 11:04:49 +0200

This seems like a silly trivial question, but I have been unable to
consume a binary stream of data as entity in a POST. I have tried with
a byte array, an InputSteam as well as accessing the entity though the
HttpServletRequest.

    @POST
    @Consumes(MediaType.APPLICATION_OCTET_STREAM)
    public Response post(byte[] payload){
        System.out.println("Payload: " + new String(payload));
        return Response.ok().build();
    }

There isn't too much info about binary entities in the wiki or
mailinglist, but my gut feeling tells me the above should be ok. So I
guess the problem then lies with my producer:

    @Test
    public void testPost() {

        byte[] payload = "This is just a dumb text used for testing
binary data".getBytes();

        ClientResponse response = resource()
                .path("/")
                .entity(payload, MediaType.APPLICATION_OCTET_STREAM)
                .post(ClientResponse.class);

        assertEquals(200, response.getStatus());
    }

I have tried many combinations of doing a POST, as well as trying to
set Content-Length etc. Before consulting an alternative HTTPClient,
can anyone spot a problem with the above code or point to an example
where Jersey retrieves raw binary data?

Thanks in advance,

/Casper