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

No available MessageBodyWriter for media type "multipart/form-data"

我正在尝试创建一个球衣客户端以通过多部分请求访问服务器。
我有要上传为 java.io.ByteArrayOutputStream 类型流的文件。
我搜索错误并发现 this question here and this answer 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)

没有处理 OutputStreamMessageBodyWriter。使用 InputStream 代替

好的,你注册MultiPartFeature.class

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

但是你必须处理和注册响应类型,假设你上传一个文件并且响应的媒体类型是json,所以你必须添加一个这样的注册;

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