I am working on spring application with angularjs. I am trying to send JSON object as a RequestBody and a RequestParam information from angularjs to spring controller, but the spring controller is not recognizing the request sent from angularjs.
js:
(function () {
'use strict';
var myApp = angular.module('app');
myApp.controller('fileWithJSONController', function ($scope, fileUploadService) {
$scope.uploadFile = function () {
var file = $scope.myFile;
var uploadUrl = myApplnURL + '/showInfo/getInformationTest';", //Url of web service
$scope.emailData = [
{ 'from':$scope.fromEmail,
'ccEmail': $scope.ccEmail,
'subject': $scope.subject,
'message':$scope.message,
}
];
var fd=new FormData();
angular.forEach($scope.files,function(file){
fd.append('file',file);
});
promise = fileWithJSONService.sendInformation(fd, $scope.emailData, uploadUrl);
promise.then(function (response) {
$scope.serverResponse = response;
}, function () {
$scope.serverResponse = 'An error has occurred';
})
};
});
})();
(function () {
'use strict';
var myApp = angular.module('app');
myApp.service('fileWithJSONService', function ($http, $q) {
this.sendInformation = function (fd, emailData, uploadUrl) {
var deffered = $q.defer();
$http.post(uploadUrl, fd, emailData, {
transformRequest: angular.identity,
headers: {'Content-Type': undefined}
}).success(function (response) {
deffered.resolve(response);
}).error(function (response) {
deffered.reject(response);
});
return deffered.promise;
}
});
})();
Spring controller:
@RequestMapping(value = "/getInformationTest", method = RequestMethod.POST, headers = {"content-type=multipart/form-data"})
public
@ResponseBody
String sendInformationTest(@RequestParam("file") List<MultipartFile> multiPartFileList,@RequestBody("emailData") EmailDataDTO emailDataDTO){
System.out.println("In spring controller");
System.out.println("single file " +multiPartFileList.size());
}
--EDITED--
PS: The path to the spring controller is correct, because when i pass only one argument from angularjs to spring controller then it is hitting the spring controller. Sample code below:
js:
....
$http.post(uploadUrl, fd , {
transformRequest: angular.identity,
headers: {'Content-Type': undefined}
})...
spring controller:
@RequestMapping(value = "/getInformationTest", method = RequestMethod.POST, headers = {"content-type=multipart/form-data"})
public
@ResponseBody
String sendInformationTest(@RequestParam("file") List<MultipartFile> multiPartFileList){
System.out.println("In spring controller");
System.out.println("single file " +multiPartFileList.size());
}
o/p:
In spring controller
single File 2
----EDITED--- I tried to change the RequestBody to RequestParam as shown in below 2 ways but both the ways are not hitting the spring controller.
First trial: js:
....
$http.post(uploadUrl, fd ,emailData, {
transformRequest: angular.identity,
headers: {'Content-Type': undefined}
})...
spring controller:
@RequestMapping(value = "/getInformationTest", method = RequestMethod.POST, headers = {"content-type=multipart/form-data"})
public
@ResponseBody
String sendInformationTest(@RequestParam("file") List<MultipartFile> multiPartFileList,@RequestParam("emailData") List<EmailDataDTO> emailData){
System.out.println("In spring controller");
}
Trial2: js:
..
$scope.emailData = [
{ 'from':$scope.fromEmail,
'ccEmail': $scope.ccEmail,
'subject': $scope.subject,
'message':$scope.message,
}
];
var fd=new FormData();
fd.append("emailData",$scope.emailData);
angular.forEach($scope.files,function(file){
fd.append('file',file);
});
promise = fileWithJSONService.sendInformation(fd, $scope.emailData, uploadUrl);
....
....//service call
$http.post(uploadUrl, fd , {
transformRequest: angular.identity,
headers: {'Content-Type': undefined}
})...
--EDITED--- I have CommonsMultipartResolver configured in servlet.xml
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="10000000"/>
</bean>
In server if you sending data in the form of JSON then change headers = {"content-type=multipart/form-data"}
to headers = {"content-type=application/json"}
and check CORS enabled or not. Change your headers from front end headers: {'Content-Type': undefined} to headers: {'Content-Type': "content-type=multipart/form-data"}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.