gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 08:23:30 35 4
gpt4 key购买 nike

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

最佳答案

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

关于java - JAX-RS 2.0 客户端 - 使用 RESTEasy 客户端发送多部分消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23632068/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com