5

I am trying to write a Jersey client app which can post multi part form data to a Restful Jersey service. I need to post a CSV file with the data and a JSON with meta-data. I am using Jersey client 1.18.3. Here is my code (some names have been changed for company confidentiality )...

Client client = Client.create();
WebResource webResource = client.resource("http://localhost:8080/mariam/service/playWithDad");


    FileDataBodyPart filePart = new FileDataBodyPart("file", 
            new File("C:/Users/Admin/Desktop/input/games.csv"));

    String playWithDadMetaJson
    = "{\n"
    + "    \"sandboxIndicator\": true,\n"
    + "    \"skipBadLines\": false,\n"
    + "    \"fileSeparator\": \"COMMA\",\n"
    + "    \"blockSize\": false,\n"
    + "    \"gameUUID\": \"43a004c9-2130-4e75-8fd4-e5fccae31840\",\n"
    + "    \"useFriends\": \"false\"\n"
    + "}\n"
    + "";

    MultiPart multipartEntity = new FormDataMultiPart()
    .field("meta", playWithDadMetaJson, MediaType.APPLICATION_JSON_TYPE)
    .bodyPart(filePart);

    ClientResponse response = webResource.type(MediaType.MULTIPART_FORM_DATA_TYPE).post(multipartEntity);

Right now I am getting a compile error at the last line saying it cannot convert from void to ClientResponse.

I got some guidance on the RestFul service itself previously from this post..

Java Rest Jersey : Posting multiple types of data (File and JSON)

Community
  • 1
  • 1
AbuMariam
  • 3,282
  • 13
  • 49
  • 82

2 Answers2

12

Follow jersey documentation, they provide sample client code. Here is the snippet to post a multipart request:

final MultiPart multiPartEntity = new MultiPart()
        .bodyPart(new BodyPart().entity("hello"))
        .bodyPart(new BodyPart(new JaxbBean("xml"), MediaType.APPLICATION_XML_TYPE))
        .bodyPart(new BodyPart(new JaxbBean("json"), MediaType.APPLICATION_JSON_TYPE));

final WebTarget target = // Create WebTarget.
final Response response = target
        .request()
        .post(Entity.entity(multiPartEntity, multiPartEntity.getMediaType()));
Denis Abakumov
  • 355
  • 3
  • 11
Juned Ahsan
  • 67,789
  • 12
  • 98
  • 136
6

"Right now I am getting a compile error at the last line saying it cannot convert from void to ClientResponse."

Look at the javadoc for WebResource. Look at the post(Object) (with Object arg). It returns void.

You need to be using the overloaded post(Class returnType, requestEntity), which return an instance of returnType type.

So you should be doing something like

ClientResponse response = webResource
        .type(MediaType.MULTIPART_FORM_DATA_TYPE)
        .post(ClientResponse.class, multipartEntity);
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • Hi Peeskillet, On the server side, the service method for this client call is public PlayWithDataVO provideService(@FormDataParam("meta") String jsonMeta, @FormDataParam("data") InputStream file), but I am seeing the InputStream param is null although the json string is coming through. Any suggestions? – AbuMariam Mar 09 '15 at 19:08
  • Never mind, I figured it out. I was missing FormDataContentDisposition which should have the same name as FormDataParam – AbuMariam Mar 09 '15 at 23:03