I am working on spring application with angularjs. I want to pass the JSON object as a @PathVariable to the spring controller, but with my below code, when passing the JSON object as a PathVariable it is not hitting the spring controller, and it is not showing any error too.Any inputs?
sample code: js:
myApp.controller('passJSONTestController', function ($rootScope, $scope, MyService) {
$scope.submitdata=function(){
$scope.myJSONData = [
{ 'name':$scope.name,
'sinNumber': $scope.sinNo,
'status': $scope.status,
'message':$scope.message,
}
];
var fd=new FormData();
angular.forEach($scope.files,function(file){
console.log("file " + file);
fd.append('file',file);
});
MyService.sendJSON($scope.myJSONData,fd).then(
function (response) {
//
},
function (errResponse) {
}
);
}
});
MyService.js
myService.sendJSON = function (myJSONData,fd) {
var deferred = $q.defer();
var myUrl = applnURL + '/dataStack/' + myJSONData + '/getAllDataInfo.form';
var config = {
transformRequest: angular.identity,
headers : {
'Content-Type': undefined
}
}
$http.post(repUrl, fd, config ).then(function (response) {
}, function (response) {
});
return deferred.promise;
}
spring controller:
@Controller
@RequestMapping("/dataStack")
public class GetData {
@RequestMapping(value = "/{myJSONData}/getAllDataInfo", method = RequestMethod.POST)
public
@ResponseBody
String sendInfo(@RequestParam("file") List<MultipartFile> multiPartFileList,@PathVariable("myJSONData") List<MyDTO> myDTO){
System.out.println("In Spring controller");
}
Why is the request passed not hitting the spring controller? PS: If i remove the pathvariable in spring controller and remove it from mySerivce.js, then it is hitting the spring controller.
-------------EDITED (Updated code)-------------- I have added the below class:
@Configuration
public class MultiFileResolverConfig {
@Bean(name = "multipartResolver")
public MultipartResolver multipartResolver() {
System.out.println("--In configuration file multipartResolver--");
return new CommonsMultipartResolver();
}
}
spring-servlet.xml
I have added below <bean> in sprig-servlet.xml configuration file
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="10000000"/>
</bean>
html code:
<html>
..elements for name,sinNumber,status,message
<input type = "file" name="file" file-model="file" multiple/>
..//submit button
</html>
js code:
myApp.directive('fileModel', ['$parse', function ($parse) {
return {
restrict: 'A',
link: function(scope, element, attrs) {
var model = $parse(attrs.fileModel);
var modelSetter = model.assign;
element.bind('change', function(){
scope.$apply(function(){
modelSetter(scope, element[0].files[0]);
});
});
}
};
}]);
myApp.controller('passJSONTestController', function ($rootScope, $scope, MyService) {
$scope.submitdata=function(){
$scope.myJSONData = [
{ 'name':$scope.name,
'sinNumber': $scope.sinNo,
'status': $scope.status,
'message':$scope.message,
}
];
var fd=new FormData();
angular.forEach($scope.files,function(file){
console.log("file " + file);
fd.append('file',file);
});
fd.append("json",$scope.myJSONData);
MyService.sendJSON(fd).then(
function (response) {
//
},
function (errResponse) {
}
);
}
});
service.js
myService.sendJSON = function (fd) {
var deferred = $q.defer();
var myUrl = applnURL + '/dataStack/getAllDataInfo.form';
var config = {
transformRequest: angular.identity,
headers : {
'Content-Type': undefined
}
}
$http.post(myUrl, fd, config ).then(function (response) {
}, function (response) {
});
return deferred.promise;
}
spring controller:
@Controller
@RequestMapping("/dataStack")
public class GetDataController{
@RequestMapping(value = "/getAllDataInfo", method = RequestMethod.POST)
public @ResponseBody
String uploadMultipleFileHandler(MultipartHttpServletRequest req) {
System.out.println("in spring upload multiple files");
List<MultipartFile> multiPartFileList = req.getFiles("file");
System.out.println("in multiPartFileList " + multiPartFileList.size());//size is always zero
String[] json = (String[]) req.getParameterMap().get("json");//getting the values submitted
//code here..
I am getting the values for json object but unable to get the files details.
The size of multiPartFileList.size() is zero.I tried adding only class MultiFileResolverConfig and removing the <bean> declaration in spring-servlet.xml but still the file details is not been passed to the spring controller.