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 客户端:发布多部分数据
基础教程推荐
- 将 double 转换为 Int,向下舍入 2022-01-01
- Java ECDSAwithSHA256 签名长度不一致 2022-01-01
- 在springboot中如何给mybatis加拦截器 2023-04-29
- 将 Windows 证书导入 Java 2022-01-01
- doFilter()是在servlet的工作完成之前还是之后执行的? 2022-01-01
- 控制台应用程序中的 Java 键盘输入解析 2022-01-01
- 如何在相机中应用自定义滤镜 [Surfaceview 预览]. 2022-01-01
- JPA惰性列表上的流 2022-01-01
- Maven:无效的目标版本:10 2022-01-01
- 在java中使用xpath和selenium解析HTML表格数据 2022-01-01
