Jersey (REST) RESPONSE with multipart/mixed with multiple bodyparts

。_饼干妹妹 提交于 2019-12-06 07:40:19

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();
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!