问题描述
我有一个基于运动衫的Web服务,它产生一个multipart / mixedRESPONSE,如下所示:该方法读取一个文件,并应以八位字节格式返回。此外,它读取该文件的元数据并以json格式返回。
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.
方法:
@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
据我所知,os.toByteArray()正在将我的对象转换为Byte数组。
As far as I know, os.toByteArray() is converting my object into Byte array.
任何想法?
编辑:我正在使用Jersey 1.15
推荐答案
错误消息你说两件事:
这是因为球衣是试穿g将 MultiPart 类型转换为可通过网络发送回客户端的二进制表示形式
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
这表示 multipart / mixed 不是有效的媒体类型
This says that multipart/mixed is NOT a valid media type
MediaType java类型对于每种接受的媒体类型都有常量...你可以使用@Produces(MediaType.MULTIPART_FORM_DATA)但这不是什么你想要
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
我认为解决方案(不是testet)可以是:
I think a solution (not testet) coud be:
1-定义一个包含的自定义类型你想要返回的每个文件都有一个byte [],类似于(使用lombok)
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-重新定义您的方法
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();
}
这篇关于泽西(REST)回应多部分/混合多个身体部位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!