JAX-RS 2.0客户端 - 使用RESTEasy客户端发送多部分消息

6

我正在使用RESTEasy客户端。
Maven依赖项:

<dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>resteasy-client</artifactId>
    <version>3.0.1.Final</version>
</dependency>

我不知道如何使用multipart调用webresource?

在服务器端,定义了以下方法:

@PUT
@Consumes(MimeHelp.MULTIPART_FORM_DATA)
@Produces(MimeHelp.JSON_UTF8)
@Path("/path")
public Response multipart(@Multipart(value = "firstPart", type = "text/plain") InputStream firstStream,
                          @Multipart(value = "secondPart", type = "text/plain") InputStream secondStream) {

现在请帮我处理客户端代码。

WebTarget target = client.target("http://localhost:8080").path("path");
//TODO somehow fill multipart
Response response = target.request().put(/*RESTEasy multipart entity or something*/);
response.close();

2
这个答案可能会对RestEasy客户端框架文件上传有所帮助。 - lefloh
1个回答

5
感谢“lefloh”评论 - 我终于完成了!
您需要添加以下Maven依赖项。
<dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>resteasy-client</artifactId>
    <version>3.0.1.Final</version>
</dependency>
<dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>resteasy-multipart-provider</artifactId>
    <version>3.0.1.Final</version>
</dependency>

以下是客户端代码:

ResteasyClient client = (ResteasyClient) this.client;
ResteasyWebTarget target = client.target("http://localhost:8080").path("path");
MultipartFormDataOutput mdo = new MultipartFormDataOutput();
mdo.addFormData("firstPart", new ByteArrayInputStream("firstContent".getBytes()), MediaType.TEXT_PLAIN_TYPE);
mdo.addFormData("secondPart", new ByteArrayInputStream("secondContent".getBytes()), MediaType.TEXT_PLAIN_TYPE);
GenericEntity<MultipartFormDataOutput> entity = new GenericEntity<MultipartFormDataOutput>(mdo) { };
Response response = target.request().put(Entity.entity(entity, MediaType.MULTIPART_FORM_DATA_TYPE));
response.close();

1
你好,有没有办法在不依赖于特定的 JAX-RS 2.0 实现的情况下完成这个功能?我需要开发一个类似的客户端,但不与任何特定实现绑定。MultipartFormDataOutput 是针对 RESTEasy 的特定实现。谢谢。 - icordoba
不,没有。据我所知,JAX-RS规范不涵盖多部分。 - bugs_

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接