10

I am building a Spring rest service for uploading a file. There is a form that consists of various field and one field for uploading a file. On submitting that form, I am sending a multipart form request i.e. Content-Type as multipart/form-data.

So I tried with below

@RequestMapping(value = "/companies", method = RequestMethod.POST)
    public void createCompany(@RequestBody CompanyDTO companyDTO, @RequestParam(value = "image", required = false) MultipartFile image){
.................   

But, the above didn't work. So for time being,i sent JSON data as String and forming Company Object from that String in rest service like

 @RequestMapping(value = "/companies", method = RequestMethod.POST)
        public void createCompany(@RequestParam("companyJson") String companyJson, @RequestParam(value = "image",required = false) MultipartFile image) throws JsonParseException, JsonMappingException, IOException{
            CompanyDTO companyDTO =  new ObjectMapper().readValue(companyJson, CompanyDTO.class);
.............................

Can't I send JSON data with @RequestBody without passing JSON as String?

Anand
  • 20,708
  • 48
  • 131
  • 198
  • Could be a duplicate of http://stackoverflow.com/questions/4083702/posting-a-file-and-data-to-restful-webservice-as-json –  Nov 16 '15 at 06:36
  • see also http://stackoverflow.com/questions/15502054/spring-mvc-requestbody-give-me-an-empty-string-when-use-with-requestparam-mult –  Nov 16 '15 at 06:37
  • 1
    The problem is `@RequestBody`, see http://stackoverflow.com/questions/29370143/spring-mvc-upload-file-with-other-fields – tungd Nov 16 '15 at 06:44
  • 1
    Without @RequestBody, objects fields are not getting mapped i.e. DTO's fields are set to null – Anand Nov 16 '15 at 08:45

3 Answers3

0

Appending the values to the URL what u have been doing now using @RequestParam.

@RequestParam annotation will not work for complex JSON Objects , it is specifi for Integer or String .

If it is a Http POST method , use of @RequestBody will make the Spring to map the incoming request to the POJO what u have created (condition: if the POJO maps the incoming JSON)

  • Multipart request is used to handle files , u can skip it off when u don't need it –  Nov 17 '15 at 08:11
0

create FormData() and append your json and file

        if (form.validate()) {
        var file = $scope.file;
        var fd = new FormData();
        fd.append('jsondata', $scope.jsonData);  
        fd.append('file', file);
        MyService.submitFormWithFile('doc/store.html', fd, '', (response){
             console.log(response)
        });
    }

//Service called in above

    MyService.submitFormWithFile = function(url, data, config, callback) {
    $http({
        method : 'POST',
        url : url,
        headers : {
            'Content-Type' : undefined
        },
        data : data,
        transformRequest : function(data, headersGetterFunction) {
            return data;
        }
    }).success(function(response, status, header, config) {
        if (status === 200) {
            callback(response);
        } else {
            console.log("error")
        }
    }).error(function(response, status, header, config) {           
            console.log(response);
    });
};

// in your java part using ObjectMapper

//it is like  string
fd.append('jsondata', JSON.stringify($scope.jsonData));



  @Autowired
  private ObjectMapper mapper;

 @RequestMapping(value = "/companies", method = RequestMethod.POST)
   public void createCompany(@RequestParam String jsondata,
        @RequestParam(required = true) MultipartFile file){


        CompanyDto companyDto=mapper.readValue(jsondata, CompanyDTO.class);
       ......
 }
Kalaiselvan
  • 2,095
  • 1
  • 18
  • 31
0

Use below code snippet:

    @RequestMapping(value= "/path", method=RequestMethod.POST, produces=MediaType.APPLICATION_JSON_VALUE)
    public ResponseObject methodName(MyData input, @RequestParam(required=false) MultipartFile file) {
        // To Do
    }
Ankit Adlakha
  • 1,436
  • 12
  • 15