Jersey客户端多部分/表单数据消息正文编写器异常

时间:2015-03-04 18:30:20

标签: java jersey-client

我觉得我非常接近,但在这里错过了一块拼图。我得到一个关于消息体编写器的例外。任何帮助表示赞赏。

服务器端代码

@POST
@Path("/{id}/documents")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
public Response uploadAndAddNewDocument(
    @FormDataParam("file") final InputStream fileInputStream,
    @FormDataParam("file") final FormDataContentDisposition fileDetail,
    @FormDataParam("file") final FormDataBodyPart fileBodyPart, 
    @FormDataParam("model") final SomeModel model,
    @FormDataParam("model") final FormDataBodyPart modelBodyPart) throws WebApplicationException {

    // Here model is received as application/json
    // Code to process model and uploaded file
    // This works when posted using POSTMAN
}

服务器代码适用于以下POSTMAN请求

POST /someresource/123/documents HTTP/1.1
Host: localhost:8080
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
Cache-Control: no-cache
Postman-Token: c6425906-1b1f-39d0-bed6-93db9e9a6ad9

----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="file"; filename="report.xml"
Content-Type: application/xml

<root></root>

----WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="doc"
Content-Type: application/json

{}
----WebKitFormBoundary7MA4YWxkTrZu0gW

泽西岛客户代码

//Client configuration code
config.getClasses().add(MultiPartWriter.class);

//Client call to server
    FormDataMultiPart multiPartFormData = new FormDataMultiPart();
    multiPartFormData.bodyPart(new BodyPart("doc", MediaType.APPLICATION_JSON_TYPE));
    multiPartFormData.bodyPart(new FileDataBodyPart("file", uploadFile));
    return service.path(uri).type(MediaType.MULTIPART_FORM_DATA).post(SomeModel.class, multiPartFormData);

**错误

Mar 03, 2015 11:55:40 PM com.sun.jersey.api.client.RequestWriter$RequestEntityWriterImpl <init>
SEVERE: A message body writer for Java class com.sun.jersey.multipart.FormDataMultiPart, and Java type class com.sun.jersey.multipart.FormDataMultiPart, and MIME media type application/octet-stream was not found
Mar 03, 2015 11:55:40 PM com.sun.jersey.api.client.RequestWriter$RequestEntityWriterImpl <init>
SEVERE: The registered message body writers compatible with the MIME media type are:
application/octet-stream ->
  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.StreamingOutputProvider
*/* ->
  com.sun.jersey.core.impl.provider.entity.FormProvider
  com.sun.jersey.core.impl.provider.entity.MimeMultipartProvider
  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.core.impl.provider.entity.XMLRootElementProvider$General
  com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$General

1 个答案:

答案 0 :(得分:0)

您需要检查的内容很少

将mimepull.jar添加到您的lib中 或与Maven

<dependency> <groupId>org.jvnet.mimepull</groupId> <artifactId>mimepull</artifactId> <version>1.9.5</version> </dependency>

如果你正在处理文件,请确保你在标题处发送 内容长度 内容类型 接受编码