@POST
@Path("/import")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadExcelFile(FormDataMultiPart form) throws Exception
I need to pass in a multi-part having two parts:a file to be uploaded and a context as a POJO. I wrote a JAX-RS (Jersey) client allowing me to test the service, here is the code:
FileDataBodyPart filePart = new FileDataBodyPart("excel-files", new File("…"));
ImportContext ic = new ImportContext(…);
MultiPart multipart = new FormDataMultiPart().field("import-context", ic, MediaType.APPLICATION_JSON_TYPE).bodyPart(filePart);
multipart.setMediaType(MediaType.MULTIPART_FORM_DATA_TYPE);
resource.type("multipart/form-data").post(Response.class, multipart);
Calling my service this way works as expected and produces the following payload:
--Boundary_1_1795960102_1504711197604
Content-Type: application/json
Content-Disposition: form-data; name="import-context"
{"createMissingBundles":"true","createMissingMessages":"true","importMode":"CREATE_AND_UPDATE","multiple":"false"}
--Boundary_1_1795960102_1504711197604
Content-Type: application/octet-stream
Content-Disposition: form-data; filename="exportTranslations.xls"; modification-date="Wed, 06 Sep 2017 12:49:07 GMT"; size=35840; name="excel-files"
Now, I need to do the same in Angular but I don't find how. I have a callback in which I enter with an event having a FormData which already contains the file to be uploaded. I need to add the context object. If I do that:
and the following exception on the server side:
java.lang.IllegalArgumentException: No available MessageBodyReader for class com.coface.corp.translationView.utils.imports.ImportContext and media type text/plain
at com.sun.jersey.multipart.BodyPart.getEntityAs(BodyPart.java:307)
...............
event.formData.append("import-context", new Blob([JSON.stringify(this.importContext)], { type: "application/json"}),"import-context");
var myReader = new FileReader();
myReader.onload = function(event){ console.log(JSON.stringify(myReader.result)); };
myReader.readAsText(myBlob);
event.formData.append("import-context", new Blob([`{"multiple":false,"createUpdate":null,"createMissingBundles":false,"createMissingMessages":false}`], { type: "application/json"}),"import-context");