Java – Jersey rest client: publish multipart data

I'm trying to write a Jersey client application that can publish multiple parts of form data to restful Jersey service I need to publish CSV files with data and JSON with metadata I'm using Jersey client 1.18 3. This is my code (some names have been changed to company confidential)

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"
    + "    \"sand@R_343_2419@Indicator\": 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);

Now I receive a compilation error on the last line saying that it cannot be converted from void to clientresponse

I got some guidance on restful services from this article before

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

Solution

View the Javadoc. Of webresource View Post (object) (with object ARG) It returns void

You need to use overloaded post (class ReturnType, requestentity), which returns an instance of ReturnType

So what you should do

ClientResponse response = webResource
        .type(MediaType.MULTIPART_FORM_DATA_TYPE)
        .post(ClientResponse.class,multipartentity);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>