2.7.11.8. 使用 RESTEasy 客户端发送多部分实体
除了配置多部分提供程序外,您还可以配置 RESTEasy 客户端来发送多部分数据。
使用 RESTEasy 客户端类
要在应用程序中使用 RESTEasy 客户端类,您必须将 Maven 依赖项添加到项目的 POM 文件中。
示例:Maven 依赖项
<dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-client</artifactId> <version>${version.org.jboss.resteasy}</version> <scope>provided</scope> </dependency> <dependency> <groupId>org.jboss.resteasy</groupId> <artifactId>resteasy-multipart-provider</artifactId> <version>${version.org.jboss.resteasy}</version> <scope>provided</scope> </dependency>
使用 RESTEasy 客户端发送多部分数据
若要发送多部分数据,您必须首先配置 RESTEasy 客户端,并构建一个 org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataOutput
对象来包含您的多部分数据。然后,您可以使用客户端将该 MultipartFormDataOutput
对象作为 javax.ws.rs.core.GenericEntity
发送。
示例:RESTEasy Client
ResteasyClient client = new ResteasyClientBuilder().build(); ResteasyWebTarget target = client.target("http://foo.com/resource"); MultipartFormDataOutput formOutputData = new MultipartFormDataOutput(); formOutputData.addFormData("part1", "this is part 1", MediaType.TEXT_PLAIN); formOutputData.addFormData("part2", "this is part 2", MediaType.TEXT_PLAIN); GenericEntity<MultipartFormDataOutput> data = new GenericEntity<MultipartFormDataOutput>(formOutputData) { }; Response response = target.request().put(Entity.entity(data, MediaType.MULTIPART_FORM_DATA_TYPE)); response.close();