如何将JSON对象作为PathVariable传递给spring控制器

我正在使用angularjs进行spring应用程序。 我想将JSON对象作为@PathVariable传递给spring控制器,但是使用我的下面的代码,当将JSON对象作为PathVariable传递时,它没有击中弹簧控制器,也没有显示任何错误。任何输入?

示例代码: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 multiPartFileList,@PathVariable("myJSONData") List myDTO){ System.out.println("In Spring controller"); } 

为什么传递的请求没有击中弹簧控制器? PS:如果我删除弹簧控制器中的pathvariable并将其从mySerivce.js中删除,那么它就会击中弹簧控制器。

————- EDITED(更新代码)————–我添加了以下类:

 @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配置文件中添加了下面的内容

    

HTML代码:

  ..elements for name,sinNumber,status,message  ..//submit button  

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 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中的声明,但仍未将文件详细信息传递给spring控制器。

更新06/22/2018

说到多部分请求,请注意Spring Boot和Spring MVC的工作方式不同。 默认情况下,Spring MVC不提供MultipartResolver,因此无法解析多部分请求。 您必须在ApplicationContext中提供“multipartResolver”bean。 但是,Spring MVC确实为此提供了两种实现方式; StandardServletMultipartResolverCommonsMultipartResolver 。 将其中任何一个配置到应用程序上下文中都可以。 例如:

 @Configuration public class SomeConfig { @Bean public MultipartResolver multipartResolver() { return new CommonsMultipartResolver(); } } 

注意:bean的名称很重要。

另一方面,Spring Boot将代表您自动配置StandardServletMultipartResolver的实例,因此能够“开箱即用”地解决多部分请求。

原始答案

您也应该使用FormData对象传递其他数据。

更改客户端代码以附加其他数据:

 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); }); // do this instead data.append('json', JSON.stringify($scope.myJSONData); ... 

在您的控制器中修复请求映射并使其接受MultipartHttpServletRequest参数而不是List

 @Controller @RequestMapping("/dataStack") public class GetData { @RequestMapping(value = "/getAllDataInfo", method = RequestMethod.POST) public @ResponseBody String sendInfo(MultipartHttpServletRequest req){ List multiPartFileList = req.getFiles("file"); ... String[] json = req.getParameterMap().get("json"); // Jackson deserialization...but you could use any...Gson, etc ObjectMapper mapper = new ObjectMapper(); TypeReference> typeRef = new TypeReference>() {}; Map data = mapper.readValue(json[0], typeRef); ... 

确保更新MyService以发布到新请求映射:

 myService.sendJSON = function (fd) { var deferred = $q.defer(); var myUrl = applnURL + '/dataStack/getAllDataInfo.form'; ... 

我认为应该为你做。

HTH

  1. 在您的客户端代码中,即MyService.js发送正确的Content-Type,在您的情况下是application / json

     headers : { 'Content-Type': application/json } 
  2. 在服务器端即spring控制器: ,通过以下方式使您的请求映射器知道传入请求的数据类型:

    @RequestMapping(value = "/{myJSONData}/getAllDataInfo", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON)

您可以使用Object(DTO)的forms发送

如下

 var createEvaluationRequestObject = {"test":"Value1","test2":"value2"}; var createEvaluationRequest = JSON.stringify (createEvaluationRequestObject); 

尝试这个,它应该工作