Search code examples
javarestjerseyjax-rsmultipart

multipart/mixed and application/octet-stream


I have a jersey based web service which produces a "multipart/mixed" RESPONSE as follows: The method reads a file, and should return it in octet format.

    @GET
        @Produces("multipart/mixed")
        public byte[] getDocumentContents(@Context HttpHeaders header){
    ....
    ....
    ....
    os = new ByteArrayOutputStream();
    ....
    ....
    ....
    return os;
    }

My question is: how do I make sure that the response is in OCTET-STREAM type? I know I could also just annotate the above method as:

@Produces("application/octet-stream")

But I specifically require to set the RESPONSE content-Type as "multipart/mixed" while sending a file in octet-stream format.

Does the above method do that ? My assumption is it does but I have not a concrete reason on how.

thank you in advance!


Solution

  • I do not think "multipart/mixed" is a valid media type to be returned by a REST method In my opinion, the correct way would be:

    @GET
    @Produces(MediaType.APPLICATION_OCTET_STREAM)
    public Response getDocumentContents(@HeaderParam("your header param") final YourHeaderParamUserType headerParam) {
        byte[] outByteArray = ... obtain byte array
        return Response.ok()
               .entity(outByteArray)
               .build();
    }
    

    Pay attention to:

    • @Produces(MediaType.APPLICATION_OCTET_STREAM)
    • The param you might want to "extract" from the header could be getted using a param in the function like:

      @HeaderParam("your header param") final YourHeaderParamUserType headerParam

    The only thing you don't have to forget in "YourHeaderParamUserType" is to:

    • Include a constructor from a string
    • ... or include a fromString(String) static method
    • ... or include a valueOf(String) static method