6

I am using RESTEasy client.
Maven dependency:

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

And I don't know how to call on webresource using multipart?

On server side is method defined like this:

@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) {

And now please help me with client code

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();
bugs_
  • 3,544
  • 4
  • 34
  • 39
  • 2
    [This answer](http://stackoverflow.com/a/19987635) to [RestEasy client framework file upload](http://stackoverflow.com/questions/10808246) might help. – lefloh May 13 '14 at 19:30

1 Answers1

5

Thanks to "lefloh" comment - I finally done it!

You have to add these maven dependencies

<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>

And here is the client code:

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();
bugs_
  • 3,544
  • 4
  • 34
  • 39
  • 1
    Hello, is there any way to do this without depending on an specific implementation of JAX-RS 2.0? I need to develop a similar cliente but not tied to any of them. MultipartFormDataOutput is specific to RESTEasy. Thanks. – icordoba Aug 09 '15 at 20:33
  • No, there is not. As far as I know. JAX-RS specification do not cover multipart. – bugs_ Aug 10 '15 at 09:58