Posting JSON file and data to Spring Service

I am creating Spring Recreation to upload a file. There is a form that consists of a different field and one field for downloading a file. When submitting this form, I submit a request with several parts, 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 did not work. So for the time being I sent the JSON data as a String and formed a Company Object from this string in the recreation service, for example

  @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 using @RequestBody without passing JSON as String?

+5
source share
3 answers

Adding values ​​to the url that you did now using @RequestParam.

@RequestParam annotation will not work for complex JSON objects; it is specified for Integer or String.

If it's an Http POST method, using @RequestBody will make Spring to map the incoming request to the POJO that you created (condition: if the POJO displays the incoming JSON)

0
source

create FormData () and add 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 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 part of Java 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); ...... } 
0
source

Use the code snippet below:

  @RequestMapping(value= "/path", method=RequestMethod.POST, produces=MediaType.APPLICATION_JSON_VALUE) public ResponseObject methodName(MyData input, @RequestParam(required=false) MultipartFile file) { // To Do } 
0
source

Source: https://habr.com/ru/post/1236024/


All Articles