I'm posting a file and a JSON object with formData from AngularJS to Spring MVC using REST.The file is well received while the JSON object keeps displaying as 'null' in the server-side.
var templateJson = {
nextIcon : '',
beforeIcon : '',
refreshIcon : '',
menuItemFont : '',
menuItemSize : '',
menuItemColor : '',
ourPromotionsFont : '',
ourPromotionsSize : '',
ourPromotionsColor : '',
backgroundBlur : '',
Menuseparator : ''
};
//after setting their values with Jquery and uploading the file I'm setting the formData.
$scope.uploadFormData = function() {
var oMyForm = new FormData();
oMyForm.append("file", file2.files[0]);
oMyForm.append('templateJson',JSON.stringify(templateJson));
$http({
method : 'POST',
url : 'uploadFile',
data: oMyForm,
headers : {
'Content-Type': undefined,
},
transformRequest : function(response, headersGetterFunction) {
headers = {
'Content-Type': undefined,
};
return response;
},
headers : {
'Content-Type' : undefined
}
}).success(function(data, headers) {
console.log(data);
alert("DataForm Sent");
}).error(function(data, headers) {
//alert("Exception" + data);
});
}
});
When i print the JSON on the console it looks well . Example :
{"nextIcon":"Icon (8).png","beforeIcon":"Icon (14).png","refreshIcon":"Icon (12).png","menuItemFont":"Verdana","menuItemSize":"11","menuItemColor":"#5ca393","ourPromotionsFont":"century schoolbook","ourPromotionsSize":"28","ourPromotionsColor":"#669999","backgroundBlur":"","Menuseparator":"separator2.png"}
For the server-side (SpringMVC) , here is the controller:
@RequestMapping(value = "/uploadFile", method=RequestMethod.POST, headers="Content-Type=multipart/form-data", consumes = consumes ={"multipart/form-data"})
public void myMethod(@RequestPart(value="templateJson",required=false) Template1 temp1, @RequestParam(value = "file") MultipartFile file
) {
System.out.println(file.getOriginalFilename());
System.out.println(temp1.getNextIcon().getBeforeIcon());
}
Template1 is a POJO :
private String nextIcon;
private String beforeIcon;
private String refreshIcon ;
private String menuItemFont ;
private String menuItemSize ;
private String menuItemColor;
private String ourPromotionsFont;
private String ourPromotionsSize;
private String ourPromotionsColor;
private String backgroundBlur;
private String Menuseparator;
//geters&setters
The Output :
"hungryeyesfood-logo.png <--- File name
null" <---- JSON
Any Idea what's going wrong with posting this JSON ?