Jersey (REST) RESPONSE with multipart/mixed with m

2019-05-20 23:43发布

问题:

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. Also, it read the metadata of that file and returns them in json form.

Method:

    @GET
            @Produces("multipart/mixed")
            public Multipart getDocumentContents(@Context HttpHeaders header){
        ....
        ....
        ....
        os = new ByteArrayOutputStream();
        ....
        ....
MultiPart multiPartEntity = new MultiPart()
            .bodyPart(new BodyPart(os.toByteArray(), MediaType.APPLICATION_OCTET_STREAM_TYPE));

            return multiPartEntity;
        }

My problem is that I get the following error which does not make sense to me:

A message body writer for Java class com.sun.jersey.multipart.MultiPart, and Java type class com.sun.jersey.multipart.MultiPart, and MIME media type multipart/mixed was not found
06.08.2013 15:32:06 com.sun.jersey.spi.container.ContainerResponse write
SCHWERWIEGEND: The registered message body writers compatible with the MIME media type are:
*/* ->
  com.sun.jersey.core.impl.provider.entity.FormProvider
  com.sun.jersey.core.impl.provider.entity.StringProvider
  com.sun.jersey.core.impl.provider.entity.ByteArrayProvider
  com.sun.jersey.core.impl.provider.entity.FileProvider
  com.sun.jersey.core.impl.provider.entity.InputStreamProvider
  com.sun.jersey.core.impl.provider.entity.DataSourceProvider
  com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$General
  com.sun.jersey.core.impl.provider.entity.ReaderProvider
  com.sun.jersey.core.impl.provider.entity.DocumentProvider
  com.sun.jersey.core.impl.provider.entity.StreamingOutputProvider
  com.sun.jersey.core.impl.provider.entity.SourceProvider$SourceWriter
  com.sun.jersey.json.impl.provider.entity.JSONJAXBElementProvider$General
  com.sun.jersey.json.impl.provider.entity.JSONArrayProvider$General
  com.sun.jersey.json.impl.provider.entity.JSONObjectProvider$General
  com.sun.jersey.json.impl.provider.entity.JSONWithPaddingProvider
  com.sun.jersey.server.impl.template.ViewableMessageBodyWriter
  com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$General
  com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$General
  com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider$General
  com.sun.jersey.json.impl.provider.entity.JSONListElementProvider$General
  com.sun.jersey.json.impl.provider.entity.JacksonProviderProxy

As far as I know, os.toByteArray() is converting my object into Byte array.

Any idea?

EDIT: I am using Jersey 1.15

回答1:

The error message says you two things:

A message body writer for Java class com.sun.jersey.multipart.MultiPart was not found

This is because jersey is trying to convert MultiPart type to a binary representation that could be sent over the wire back to the client

MIME media type multipart/mixed was not found

This says that multipart/mixed is NOT a valid media type

MediaType java type has constants for every accepted media type... you can use @Produces(MediaType.MULTIPART_FORM_DATA) but this is not what you want

I think a solution (not testet) coud be:

1- Define a custom type containing a byte[] for every file you want to return, something like (using lombok)

@XmlRootElement(name="returnedFiles")
@Accessors(prefix="_")
@NoArgsConstructor
public class ReturnedFiles {
    @XmlElement(name="file1Contents")
    @Getter @Setter private byte[] _file1Contents;
    @XmlElement(name="file1Name")
    @Getter @Setter private String _file1Name;

    @XmlElement(name="file1Contents")
    @Getter @Setter private byte[] _file2Contents;
    @XmlElement(name="file1Name")
    @Getter @Setter private String _file2Name;
}

2- Redefine your method

@GET @Path("{myPath}") 
@Produces(MediaType.APPLICATION_XML)
public Response getDocumentContents(@Context HttpHeaders header) {
    ... load the files and compose the ReturnedFiles instance
    return Response.ok()
                   .entity(returnedFilesInstance)
                   .build();
}