【发布时间】:2018-11-30 06:55:28
【问题描述】:
我正在使用 angularjs 开发 Spring 应用程序。我想将 JSON 对象作为 @PathVariable 传递给 spring 控制器,但是使用下面的代码,当将 JSON 对象作为 PathVariable 传递时,它没有命中 spring 控制器,也没有显示任何错误。任何输入?
示例代码: 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;
}
弹簧控制器:
@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");
}
为什么传递的请求没有命中spring控制器? PS:如果我删除 spring 控制器中的路径变量并将其从 mySerivce.js 中删除,那么它正在命中 spring 控制器。
-------------已编辑(更新代码)------------- 我添加了以下类:
@Configuration
public class MultiFileResolverConfig {
@Bean(name = "multipartResolver")
public MultipartResolver multipartResolver() {
System.out.println("--In configuration file multipartResolver--");
return new CommonsMultipartResolver();
}
}
spring-servlet.xml
我在 sprig-servlet.xml 配置文件下面添加了<bean>
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="10000000"/>
</bean>
html代码:
<html>
..elements for name,sinNumber,status,message
<input type = "file" name="file" file-model="file" multiple/>
..//submit button
</html>
js代码:
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;
}
弹簧控制器:
@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..
我正在获取 json 对象的值,但无法获取文件详细信息。
multiPartFileList.size() 的大小为零。我尝试仅添加类 MultiFileResolverConfig 并删除 spring-servlet.xml 中的 <bean> 声明,但文件详细信息仍未传递给 spring 控制器。
【问题讨论】:
-
为什么你通过带有变量的json对象,你应该去@RequestParam或者最好的方法是使用POST并通过requestbody
-
我尝试使用 post 并通过 RequestBody,如果您在上面的代码中看到我的 post 方法,我正在传递 url、fd、config 信息,并且当我将此 Json 对象添加为其他参数并修改 spring 控制器以接受时RequestBody 它仍然无法识别弹簧控制器...
-
您也必须更改请求映射 url。