Jersey 客户端异常:未找到消息正文编写器 [英] Jersey client exception: A message body writer was not found

查看:42
本文介绍了Jersey 客户端异常:未找到消息正文编写器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Jersey 客户端访问 PHP Web 服务以实现图像上传功能.我收到以下异常:

I am using Jersey client to hit a PHP web service for image uploading functionality. I am getting the following exception:

Caused by: com.sun.jersey.api.client.ClientHandlerException: 
A message body writer for Java type, class 
com.sun.jersey.multipart.FormDataMultiPart, and MIME media type, 
multipart/form-data, was not found
    at com.sun.jersey.api.client.RequestWriter.writeRequestEntity(RequestWriter.java:288)
    at com.sun.jersey.client.urlconnection.URLConnectionClientHandler._invoke(URLConnectionClientHandler.java:204)
    at com.sun.jersey.client.urlconnection.URLConnectionClientHandler.handle(URLConnectionClientHandler.java:147)
    ... 63 more

这是我使用的代码:

WebResource webResource = Client.create().resource(HTTP_REST_URI);
JSONObject jSONObj = webResource.queryParams(queryParams)
      .type(MediaType.MULTIPART_FORM_DATA)
      .post(JSONObject.class, formDataMultiPart);

如何解决这个异常?

推荐答案

在创建 Client 时注册 MultiPartWriter 提供程序:

Register the MultiPartWriter provider when creating the Client:

ClientConfig cc = new DefaultClientConfig();
Client client;

cc.getClasses().add(MultiPartWriter.class);
client = Client.create(cc);

如果使用 Maven,这些是您在 pom.xml 中需要的依赖项:

If using Maven, these are the dependencies you need in your pom.xml:

<dependency>
    <groupId>com.sun.jersey</groupId>
    <artifactId>jersey-client</artifactId>
    <version>1.17.1</version>
</dependency>
<dependency>
    <groupId>com.sun.jersey.contribs</groupId>
    <artifactId>jersey-multipart</artifactId>
    <version>1.17.1</version>
</dependency>

这篇关于Jersey 客户端异常:未找到消息正文编写器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆