加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – Jersey REST Client:发布MultiPart数据

发布时间:2020-12-15 05:12:38 所属栏目:Java 来源:网络整理
导读:我正在尝试编写一个Jersey客户端应用程序,它可以将多部分表单数据发布到Restful Jersey服务.我需要发布带有数据的CSV文件和带有元数据的 JSON.我正在使用Jersey客户端1.18.3.这是我的代码(一些名称已被更改为公司保密)… Client client = Client.create();We
我正在尝试编写一个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);

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

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

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

解决方法

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

查看WebResource的javadoc.查看post(Object) (with Object arg).它返回void.

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

所以你应该做的事情

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读