2

I have a JAX-RS server implementation, it uses the following Bean Object to pass request data, lets call it DisputeComment having the following parameters

public class DisputeComment {

    private String id;
    private String dispute;
    private String user;
    private String admin;
    private String comment;
    private String date;
}

This is how my HTTP post request is defined in JAX - RS

@POST
    @Path("/disputes/{id}/comments")
    @Consumes({ "multipart/mixed;type=application/json;charset=UTF-8" /* , MediaType.APPLICATION_XML */})
    @Produces({ "multipart/mixed;type=application/json;charset=UTF-8" /* , MediaType.APPLICATION_XML */ })
    GetCommentsResponse addComment(
            @PathParam("id")
            @Pattern(regexp = "[0-9]+", message = "The id must be a valid number")
            @NotNull
                    String disputeId, DisputeCommentRequest request) throws TechnicalWsFault;

This work really well until now.

But now, I want to adapt this HTTP request to be able to pass along a text file. I have tried somethings, but it doesn't seem to work, here is what I tried. I added an extra parameter InputStream and changed the Consumes annotation.

public class DisputeComment {

        private String id;
        private String dispute;
        private String user;
        private String admin;
        private String comment;
        private String date;
        private InputStream inputStream;

    }
@Consumes({"multipart/mixed;type=application/json;charset=UTF-8"})

Is this supposed to work? I don't even know how to test this? It would be great if someone can tell me what should I change in my code so that it accepts a file and the rest of the json object? And how do I write the client test code. This is my code which is testing the HTTP post without the upload file feature

WebClient wc = getClient();
        DisputeCommentRequest disputeCommentRequest = new DisputeCommentRequest();
        disputeCommentRequest.setUser(MockData.GMID);
        disputeCommentRequest.setComment(MockData.DISPUTE_COMMENT);
        Response res = wc.path("disputes/" + MockData.DISPUTE_ID + "/comments").accept("multipart/mixed;type=application/json;charset=UTF-8").type("multipart/mixed;type=application/json;charset=UTF-8").post(disputeCommentRequest);
        res.bufferEntity();
Nitesh
  • 163
  • 1
  • 13
  • 1
    I suggest posting to your service via curl then updating your question with your testing results. https://curl.haxx.se/ – Andreas Jul 26 '17 at 15:42
  • I don't yet know how to prepare a client request which can do that? For the moment, i'm still confused about Content-Type, if I can re-use the same bean object to pass the file? – Nitesh Jul 26 '17 at 15:43
  • If you're asking how to post a file with curl, there is some pretty good documentation: https://ec.haxx.se/http-multipart.html and a number of highly voted SO answers https://stackoverflow.com/a/12667839/3430807. I don't see how you can be expected to write the service if you're at a loss for how it will be called. – Andreas Jul 26 '17 at 15:51

2 Answers2

0

I faced exactly the same issue in one of my project. I was supposed to send pictures. After hours trying to deal with multipart I found a simple solution (which can match your need as I understand you want to send just a text file). You can encode in base64 the file and send it as a field in your post request. Then you decode it on your backend.

{
    id:"",
    dispute:"",
    user:"",
    admin:"",
    comment:"",
    date:"",
    fileBase64:""
}

For information Google is using this in their vision API.

Good luck

Geoffrey
  • 833
  • 1
  • 9
  • 20
0

You have the option of reading the POST data yourself. I can't tell how you are sending your data, but you can use HttpServletRequest.getReader(). If all you are sending is the file, then you can simply read the file and store it as a String or write it into a File if it is long. If you are sending other information as well, that will involve some parsing on your part.

Steve11235
  • 2,673
  • 1
  • 15
  • 18