2015-11-16 10 views
5

Sto costruendo un servizio di riposo a molla per il caricamento di un file. Esiste un modulo composto da vari campi e un campo per il caricamento di un file. Al momento dell'invio di tale modulo, invierò una richiesta di modulo in più parti, ovvero Content-Type come multipart/form-data.Invio di un file e dati JSON al servizio di riposo a primavera

Così ho provato con sotto

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

Ma, quanto sopra non ha funzionato. Quindi, per essere tempo, ho inviato i dati JSON as String e formare oggetto sociale da quella stringa in servizio resto come

@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); 
............................. 

non posso inviare dati JSON con @RequestBody senza passare JSON as String?

+0

potrebbe essere un duplicato di http://stackoverflow.com/questions/4083702/posting-a-file-and-data-to-restful-webservice-as-json –

+0

vedi anche http: // StackOverflow. it/questions/15502054/spring-mvc-requestbody-give-me-an-empty-string-when-use-with-requestparam-mult –

+1

Il problema è '@ RequestBody', vedere http://stackoverflow.com/questions/29370143/spring-mvc-upload-file-with-other-fields – tungd

risposta

0

Aggiunta dei valori all'URL che cosa state facendo ora usando @RequestParam.

L'annotazione @RequestParam non funziona per oggetti JSON complessi, è specifica per Integer o String.

Se si tratta di un metodo HTTP POST, uso di @RequestBody farà la primavera per mappare la richiesta in arrivo al POJO che u hanno creato (condizione: se il POJO mappa il JSON in entrata)

+0

nullo ma non funziona con la richiesta Multipart – Anand

+0

La richiesta multipart è utilizzata per gestire file, puoi saltarlo quando non ne hai bisogno – KaMaL

+0

come saltarlo? – Anand

0

creare formdata () e aggiungere il tuo jSON e 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) 
     }); 
    } 

// servizio chiamato al precedente

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); 
    }); 
}; 

// nella vostra parte Java utilizzando 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); 
     ...... 
} 
Problemi correlati