媒体类型没有可用的MessageBodyWriter" multipart / form-data"

时间:2017-03-14 19:05:03

标签: java tomcat multipartform-data jersey-client multipartentity

我试图创建一个针对具有多部分请求的服务器的泽西客户端 我将文件上传为java.io.ByteArrayOutputStream类型的流。
我搜索了错误并遇到了this question hereanswer here,并添加了 创建register(MultiPartFeature.class)Client,但无效。 我可能只需要朝着正确的方向轻推。谢谢!

我没有使用Maven或其他任何东西。

我的java文件中的相关部分是:

FormDataMultiPart multiPart = new FormDataMultiPart();
FormDataContentDisposition.FormDataContentDispositionBuilder dispositionBuilder = FormDataContentDisposition
 .name("file");

dispositionBuilder.fileName("file.zip");
FormDataContentDisposition formDataContentDisposition = dispositionBuilder.build();

/*
 * byteArrayOutputStream is of type java.io.ByteArrayOutputStream
 * and contains the file I wish to upload.
 */
multiPart.bodyPart(
 new FormDataBodyPart("file", byteArrayOutputStream, MediaType.APPLICATION_OCTET_STREAM_TYPE)
 .contentDisposition(formDataContentDisposition));

Entity < FormDataMultiPart > entity = Entity.entity(multiPart, MediaType.MULTIPART_FORM_DATA_TYPE);

Client client = ClientBuilder.newClient(new ClientConfig().register(MultiPartFeature.class));
WebTarget webTarget = client.target("http://localhost:3000/path/to/service");

// send request

Invocation.Builder invocationBuilder = webTarget.request();
Response response = invocationBuilder.post(entity);
return response;

web.xml的相关部分是:

          <servlet>
              <servlet-name>Jersey Web Application</servlet-name>
              <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
              <init-param>
                     <param-name>jersey.config.server.provider.packages</param-name>
                     <param-value>com.example</param-value>
              </init-param>
              <init-param>
                     <param-name>jersey.config.server.provider.classnames</param-name>
                     <param-value>org.glassfish.jersey.filter.LoggingFilter;org.glassfish.jersey.media.multipart.MultiPartFeature</param-value>
              </init-param>
              <load-on-startup>1</load-on-startup>
       </servlet>

我尝试在Apache Tomcat 8.5运行时服务器上运行Eclipse时遇到以下错误。

SEVERE: Servlet.service() for servlet [Jersey Web Application] in context with path [/TestRest] threw exception [javax.ws.rs.ProcessingException: No available MessageBodyWriter for class "class java.io.ByteArrayOutputStream" and media type "multipart/form-data".] with root cause
java.lang.IllegalArgumentException: No available MessageBodyWriter for class "class java.io.ByteArrayOutputStream" and media type "multipart/form-data".
    at org.glassfish.jersey.media.multipart.internal.MultiPartWriter.writeTo(MultiPartWriter.java:229)
    at org.glassfish.jersey.media.multipart.internal.MultiPartWriter.writeTo(MultiPartWriter.java:79)
    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.invokeWriteTo(WriterInterceptorExecutor.java:265)
    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor$TerminalWriterInterceptor.aroundWriteTo(WriterInterceptorExecutor.java:250)
    at org.glassfish.jersey.message.internal.WriterInterceptorExecutor.proceed(WriterInterceptorExecutor.java:162)
    at org.glassfish.jersey.message.internal.MessageBodyFactory.writeTo(MessageBodyFactory.java:1130)

2 个答案:

答案 0 :(得分:1)

没有MessageBodyWriter处理OutputStream。请改用InputStream

答案 1 :(得分:0)

好,您注册MultiPartFeature.class

  

Client client = ClientBuilder.newClient(new ClientConfig()。register(MultiPartFeature.class));

但是您必须处理和注册响应类型,假设您上载了一个文件,并且响应的媒体类型是json,因此必须添加一个这样的寄存器;

  

Client client = ClientBuilder.newClient(new ClientConfig()。register(MultiPartFeature.class).register(JacksonFeature.class));