Rectangle 27 0

java How to process a multipart request consisting of a file and a JSON object in Spring restful service?


@RequestMapping(value="/create", method=RequestMethod.POST, headers="content-type=application/json,application/java-archive")

The default content type is 'application/octet-stream'. Since you are uploading jar file and JSON the content type should be set in the @RequestMapping annotation as follows:

Note
Rectangle 27 0

java How to process a multipart request consisting of a file and a JSON object in Spring restful service?


--MyBoundary
Content-Disposition: file; filename="image.gif"
Content-Type: image/gif
Content-Transfer-Encoding: binary

...contents of image.gif...
--MyBoundary--
Content-Type: multipart/form-data; boundary=MyBoundary

--MyBoundary
Content-Disposition: form-data; name="myJsonString"
Content-Type: application/json

MyJsonObj //Your Json Object goes here
--MyBoundary
Content-Disposition: form-data; name="files"; filename="myfile.txt"
Content-Type: text/plain

... contents of myfile.txt ...
--MyBoundary--
  • Be consistent and use the same value all over the request.
  • Use a value that won't appear in the HTTP data sent to the server like 'AaB03x'.

Hope this should help you. You need to set the boundary in your request to inform the HTTP Request. is simple; A brief introduction to the multipart format can be found in the below link

The following example illustrates "multipart/form-data" encoding. If the Json Object is "MyJsonObj" , and file that need to be send is "myfile.txt", the user agent might send back the following data:

You specify boundary in the Content-Type header so that the server knows how to split the data sent.

or if your files is of type image with name "image.gif" then,

Note
Rectangle 27 0

java How to process a multipart request consisting of a file and a JSON object in Spring restful service?


JSON.stringify(json)
@RequestMapping(value = "/url", method = RequestMethod.POST)
public final ModelAndView uploadJson(@RequestBody JsonObject json) throws Exception {
var uploadJson = function (json) {
    $.ajax({
        dataType: "html",
        contentType: "application/json; charset=utf-8",
        method: "POST",
        url: "url.do",
        data: JSON.stringify(json),
        success: function (data) {

        },
        error: function (data) {

        }
    });
}

And like I said, on the server side nothing special

I dont think that even possible, or at least I dont know how

In my case that I send my JSON using an ajax call and specifying the format. In the server side I just need to wait for the object already deserialized by Jackson.

but I want to send a file along with the JSON object (in the same request).

Note
Rectangle 27 0

java How to process a multipart request consisting of a file and a JSON object in Spring restful service?


Another problem is that the JSON is uploaded as a (regular) value of a text input in the form, not as a separate part in the multi-part/MIME. So there's no content-type header associated with it to find out that Spring should use the JSON deserializer. You can use @RequestParam instead and register a specific converter like in this answer: JSON parameter in spring MVC controller

So, first try setting the same name for the parameter and the form's input element.

The error message indicates that there is no HttpMessageConverter registered for a multi-part/MIME part of content type: application/octet-stream. Still, your jarFile parameter is most likely correctly identified as application/octet-stream, so I'm assuming there's a mismatch in the parameter mapping.

Note
Rectangle 27 0

java How to process a multipart request consisting of a file and a JSON object in Spring restful service?


Have a look at the following url, it does something similar to what you want. Although, it is only passing an additional string parameter as well as a file. There is no reason why it couldn't be a more complex type and let Jackson de-serialise it.

They are using the annotation @RequestParam rather than the one you are using.

Note
Rectangle 27 0

java How to process a multipart request consisting of a file and a JSON object in Spring restful service?


$scope.saveForm = function () {
      var formData = new FormData();
      var file = $scope.myFile;
      var json = $scope.myJson;
      formData.append("file", file);
      formData.append("ad",JSON.stringify(json));//important: convert to string JSON!
      var req = {
        url: '/upload',
        method: 'POST',
        headers: {'Content-Type': undefined},
        data: formData,
        transformRequest: function (data, headersGetterFunction) {
          return data;
        }
      };
@RequestMapping
@RequestMapping(value = "/upload", method = RequestMethod.POST)
    public @ResponseBody
    Advertisement storeAd(@RequestPart("ad") String adString, @RequestPart("file") MultipartFile file) throws IOException {

        Advertisement jsonAd = new ObjectMapper().readValue(adString, Advertisement.class);
//do whatever you want with your file and jsonAd
consumes = MediaType.MULTIPART_FORM_DATA_VALUE

Helped me sort out my issue too.. Thanks a lot @user3207391

Note
Rectangle 27 0

java How to process a multipart request consisting of a file and a JSON object in Spring restful service?


$scope.saveForm = function () {
      var formData = new FormData();
      var file = $scope.myFile;
      var json = $scope.myJson;
      formData.append("file", file);
      formData.append("ad",JSON.stringify(json));//important: convert to string JSON!
      var req = {
        url: '/upload',
        method: 'POST',
        headers: {'Content-Type': undefined},
        data: formData,
        transformRequest: function (data, headersGetterFunction) {
          return data;
        }
      };
@RequestMapping
@RequestMapping(value = "/upload", method = RequestMethod.POST)
    public @ResponseBody
    Advertisement storeAd(@RequestPart("ad") String adString, @RequestPart("file") MultipartFile file) throws IOException {

        Advertisement jsonAd = new ObjectMapper().readValue(adString, Advertisement.class);
//do whatever you want with your file and jsonAd
consumes = MediaType.MULTIPART_FORM_DATA_VALUE

Helped me sort out my issue too.. Thanks a lot @user3207391

Note
Rectangle 27 0

java How to process a multipart request consisting of a file and a JSON object in Spring restful service?


--MyBoundary
Content-Disposition: file; filename="image.gif"
Content-Type: image/gif
Content-Transfer-Encoding: binary

...contents of image.gif...
--MyBoundary--
Content-Type: multipart/form-data; boundary=MyBoundary

--MyBoundary
Content-Disposition: form-data; name="myJsonString"
Content-Type: application/json

MyJsonObj //Your Json Object goes here
--MyBoundary
Content-Disposition: form-data; name="files"; filename="myfile.txt"
Content-Type: text/plain

... contents of myfile.txt ...
--MyBoundary--
  • Be consistent and use the same value all over the request.
  • Use a value that won't appear in the HTTP data sent to the server like 'AaB03x'.

Hope this should help you. You need to set the boundary in your request to inform the HTTP Request. is simple; A brief introduction to the multipart format can be found in the below link

The following example illustrates "multipart/form-data" encoding. If the Json Object is "MyJsonObj" , and file that need to be send is "myfile.txt", the user agent might send back the following data:

You specify boundary in the Content-Type header so that the server knows how to split the data sent.

or if your files is of type image with name "image.gif" then,

Note
Rectangle 27 0

java How to process a multipart request consisting of a file and a JSON object in Spring restful service?


--MyBoundary
Content-Disposition: file; filename="image.gif"
Content-Type: image/gif
Content-Transfer-Encoding: binary

...contents of image.gif...
--MyBoundary--
Content-Type: multipart/form-data; boundary=MyBoundary

--MyBoundary
Content-Disposition: form-data; name="myJsonString"
Content-Type: application/json

MyJsonObj //Your Json Object goes here
--MyBoundary
Content-Disposition: form-data; name="files"; filename="myfile.txt"
Content-Type: text/plain

... contents of myfile.txt ...
--MyBoundary--
  • Be consistent and use the same value all over the request.
  • Use a value that won't appear in the HTTP data sent to the server like 'AaB03x'.

Hope this should help you. You need to set the boundary in your request to inform the HTTP Request. is simple; A brief introduction to the multipart format can be found in the below link

The following example illustrates "multipart/form-data" encoding. If the Json Object is "MyJsonObj" , and file that need to be send is "myfile.txt", the user agent might send back the following data:

You specify boundary in the Content-Type header so that the server knows how to split the data sent.

or if your files is of type image with name "image.gif" then,

Note
Rectangle 27 0

java How to process a multipart request consisting of a file and a JSON object in Spring restful service?


JSON.stringify(json)
@RequestMapping(value = "/url", method = RequestMethod.POST)
public final ModelAndView uploadJson(@RequestBody JsonObject json) throws Exception {
var uploadJson = function (json) {
    $.ajax({
        dataType: "html",
        contentType: "application/json; charset=utf-8",
        method: "POST",
        url: "url.do",
        data: JSON.stringify(json),
        success: function (data) {

        },
        error: function (data) {

        }
    });
}

And like I said, on the server side nothing special

I dont think that even possible, or at least I dont know how

In my case that I send my JSON using an ajax call and specifying the format. In the server side I just need to wait for the object already deserialized by Jackson.

but I want to send a file along with the JSON object (in the same request).

Note