Jersey REST 客户端:发布多部分数据

我正在尝试编写一个 Jersey 客户端应用程序,它可以将多部分表单数据发布到 Restful Jersey 服务.我需要发布一个包含数据的 CSV 文件和一个包含元数据的 JSON.我正在使用泽西客户端 1.18.3.这是我的代码(某些名称已更改为公司机密)...

I am trying to write a Jersey client app which can post multi part form data to a Restful Jersey service. I need to post a CSV file with the data and a JSON with meta-data. I am using Jersey client 1.18.3. Here is my code (some names have been changed for company confidentiality )...

Client client = Client.create();
WebResource webResource = client.resource("http://localhost:8080/mariam/service/playWithDad");


    FileDataBodyPart filePart = new FileDataBodyPart("file", 
            new File("C:/Users/Admin/Desktop/input/games.csv"));

    String playWithDadMetaJson
    = "{
"
    + "    "sandboxIndicator": true,
"
    + "    "skipBadLines": false,
"
    + "    "fileSeparator": "COMMA",
"
    + "    "blockSize": false,
"
    + "    "gameUUID": "43a004c9-2130-4e75-8fd4-e5fccae31840",
"
    + "    "useFriends": "false"
"
    + "}
"
    + "";

    MultiPart multipartEntity = new FormDataMultiPart()
    .field("meta", playWithDadMetaJson, MediaType.APPLICATION_JSON_TYPE)
    .bodyPart(filePart);

    ClientResponse response = webResource.type(MediaType.MULTIPART_FORM_DATA_TYPE).post(multipartEntity);

现在我在最后一行收到一个编译错误,说它无法从 void 转换为 ClientResponse.

Right now I am getting a compile error at the last line saying it cannot convert from void to ClientResponse.

我之前从这篇文章中得到了一些关于 RestFul 服务本身的指导.

I got some guidance on the RestFul service itself previously from this post..

Java Rest Jersey:发布多种类型数据(文件和 JSON)

推荐答案

现在我在最后一行收到一个编译错误,说它无法从 void 转换为 ClientResponse."

查看 <代码>WebResource.查看 post(Object)(带有 Object arg).它返回 void.

Look at the javadoc for WebResource. Look at the post(Object) (with Object arg). It returns void.

您需要使用重载的 post(Class returnType, requestEntity),返回一个returnType的实例输入.

You need to be using the overloaded post(Class returnType, requestEntity), which return an instance of returnType type.

所以你应该做类似的事情

So you should be doing something like

ClientResponse response = webResource
        .type(MediaType.MULTIPART_FORM_DATA_TYPE)
        .post(ClientResponse.class, multipartEntity);

相关文章