Jersey客户端异常:消息正文作家没有被发现(Jersey client exception: A

2019-07-22 21:03发布

我使用Jersey客户端打的图片上传功能的PHP Web服务。 我得到以下异常:

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);

这怎么能例外解决?

Answer 1:

注册MultiPartWriter创建时提供Client

ClientConfig cc = new DefaultClientConfig();
Client client;

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

如果使用Maven,这些都是你在你需要的依赖关系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>


Answer 2:

泽西(服务器或客户端)具有支持一个Java类型的转换到一个流,反之亦然提供商。 你的代码返回(或接收)的Java对象和基于对象和您正在使用的内容类型的类型,泽西岛寻找合适的供应商做编组(或解组)。

该供应商实现化MessageBodyReader或MessageBodyWriter接口,并为每个Java类型和内容类型组合应用程序使用,你必须有一个知道如何处理相结合的供应商。

你得到的消息告诉你,新泽西找不到知道如何封送提供商FormDataMultiPart对象与multipart/form-data MIME类型。 您需要提供一个,如果我没有记错的默认实现在发现jersey-multipart.jarmimepull.jar文件 。



Answer 3:

我面临着同样的问题。 它得到了改变行家依赖于新泽西州的多罐子从1.0.2到1.8版本(用于客户端相同的依赖以及提供方解决。

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

下面是代码的完整的一块,我正在使用

File file = new File("E:/Goodies/tmp/sparrow.jpg");
byte[] logo = FileUtils.readFileToByteArray(file);

MultiPart multiPart = new MultiPart().bodyPart(new BodyPart(logo, MediaType.APPLICATION_OCTET_STREAM_TYPE));

// POST the request
try{
ClientResponse response = service.type("multipart/mixed").post(ClientResponse.class, multiPart);
System.out.println("Response Status : " + response.getEntity(String.class));
}catch(Exception e){
    e.printStackTrace();
}

而在web服务:

@POST
@Consumes("multipart/mixed")
@Path("/upload")
public Response post(MultiPart multiPart) {

    BodyPartEntity bpe = (BodyPartEntity) multiPart.getBodyParts().get(0)
            .getEntity();

    boolean isProcessed = false;
    String message = null;
    try {
        InputStream source = bpe.getInputStream();
        BufferedImage bi = ImageIO.read(source);

        File file = new File("E:/Goodies/tmp" + "123.jpg");

        // storing the image to file system.
        if (file.isDirectory()) {
            ImageIO.write(bi, "jpg", file);
        } else {
            file.mkdirs();
            ImageIO.write(bi, "jpg", file);
        }
        isProcessed = true;
    } catch (Exception e) {
        message = e.getMessage();
    }


Answer 4:

还有,你需要检查几件事情

添加mimepull.jar到库或使用Maven

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

编码接受 ,如果你与文件工作,确保您在头发送内容长度内容类型



Answer 5:

我添加了这个web.xml 。 问题解决了。

<init-param>
   <param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
   <param-value>true</param-value>
</init-param>


Answer 6:

Here's my work around:

        WebResource webResource =
                jerseyClient.resource("www.api.com");
        WebResource.Builder requestBuilder = webResource.getRequestBuilder();
        requestBuilder.header("content-type", "application/json");
        ClientResponse response = requestBuilder
                .post(ClientResponse.class, mObjectMapper.writeValueAsString(new RequestObject(longUrl)));
        String text = response.getEntity(String.class);
        ResponseObject outcome = mObjectMapper.readValue(text, ResponseObject.class);

I have used Jackson ObjectMapper to serialize the request payload and likewise deserialized the outcome into a ResponseObject instance using ObjectMapper.



文章来源: Jersey client exception: A message body writer was not found