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?
potrebbe essere un duplicato di http://stackoverflow.com/questions/4083702/posting-a-file-and-data-to-restful-webservice-as-json –
vedi anche http: // StackOverflow. it/questions/15502054/spring-mvc-requestbody-give-me-an-empty-string-when-use-with-requestparam-mult –
Il problema è '@ RequestBody', vedere http://stackoverflow.com/questions/29370143/spring-mvc-upload-file-with-other-fields – tungd