Jersey REST Client : Posting MultiPart data(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);
这篇关于Jersey REST 客户端:发布多部分数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Jersey REST 客户端:发布多部分数据
- Safepoint+stats 日志,输出 JDK12 中没有 vmop 操作 2022-01-01
- value & 是什么意思?0xff 在 Java 中做什么? 2022-01-01
- 如何使用WebFilter实现授权头检查 2022-01-01
- 从 finally 块返回时 Java 的奇怪行为 2022-01-01
- Jersey REST 客户端:发布多部分数据 2022-01-01
- Eclipse 插件更新错误日志在哪里? 2022-01-01
- 将log4j 1.2配置转换为log4j 2配置 2022-01-01
- Spring Boot连接到使用仲裁器运行的MongoDB副本集 2022-01-01
- Java包名称中单词分隔符的约定是什么? 2022-01-01
- C++ 和 Java 进程之间的共享内存 2022-01-01
