【问题标题】:JAX-RS 2.0 Client - send multipart message with RESTEasy clientJAX-RS 2.0 客户端 - 使用 RESTEasy 客户端发送多部分消息
【发布时间】:2014-05-13 13:03:18
【问题描述】:

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

【问题讨论】:

标签: java rest client jax-rs resteasy


【解决方案1】:

感谢“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();

【讨论】:

  • 您好,有什么方法可以在不依赖 JAX-RS 2.0 的具体实现的情况下做到这一点?我需要开发一个类似的客户,但不依赖于他们中的任何一个。 MultipartFormDataOutput 特定于 RESTEasy。谢谢。
  • 不,没有。我所知道的。 JAX-RS 规范不涵盖多部分。
猜你喜欢
  • 2019-09-25
  • 2014-07-11
  • 2023-03-20
  • 2019-04-19
  • 2023-04-04
  • 1970-01-01
  • 2018-01-19
  • 2017-05-05
  • 2016-01-10
相关资源
最近更新 更多