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

时间:2014-05-13 13:03:19

标签: java rest client jax-rs resteasy

我正在使用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();

1 个答案:

答案 0 :(得分:3)

感谢&#34; lefloh&#34;评论 - 我终于做到了!

您必须添加这些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();