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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28754766/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 06:44:53  来源:igfitidea点击:

Jersey REST Client : Posting MultiPart data

javarestjerseymultipartform-datajersey-client

提问by AbuMariam

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

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

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
    = "{\n"
    + "    \"sandboxIndicator\": true,\n"
    + "    \"skipBadLines\": false,\n"
    + "    \"fileSeparator\": \"COMMA\",\n"
    + "    \"blockSize\": false,\n"
    + "    \"gameUUID\": \"43a004c9-2130-4e75-8fd4-e5fccae31840\",\n"
    + "    \"useFriends\": \"false\"\n"
    + "}\n"
    + "";

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

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

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

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

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

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

Java Rest Jersey : Posting multiple types of data (File and JSON)

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

采纳答案by Paul Samsotha

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

“现在我在最后一行收到一个编译错误,说它无法从 转换voidClientResponse。”

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

查看 javadoc 的WebResource. 看看post(Object)(with Object arg)。它返回无效。

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

您需要使用重载post(Class returnType, requestEntity),它返回一个returnType类型的实例。

So you should be doing something like

所以你应该做类似的事情

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

回答by Juned Ahsan

Follow jersey documentation, they provide sample client code. Here is the snippet to post a multipart request:

按照jersey 文档,他们提供示例客户端代码。这是发布多部分请求的代码段:

final MultiPart multiPartEntity = new MultiPart()
        .bodyPart(new BodyPart().entity("hello"))
        .bodyPart(new BodyPart(new JaxbBean("xml"), MediaType.APPLICATION_XML_TYPE))
        .bodyPart(new BodyPart(new JaxbBean("json"), MediaType.APPLICATION_JSON_TYPE));

final WebTarget target = // Create WebTarget.
final Response response = target
        .request()
        .post(Entity.entity(multiPartEntity, multiPartEntity.getMediaType()));