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

时间:2018-06-21 03:52:01

标签: java json spring angular

我正在使用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控制器中删除了pathvariable并将其从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 我在<bean>下面的sprig-servlet.xml配置文件中添加了

<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控制器。

3 个答案:

答案 0 :(得分:1)

  

更新06/22/2018

在涉及多部分请求时,请注意,Spring Boot和Spring MVC的工作方式有所不同。默认情况下,Spring MVC不会提供 ,因此不能解析多部分请求。您必须在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<MultipartFile>

@Controller
@RequestMapping("/dataStack")
public class GetData {
 @RequestMapping(value = "/getAllDataInfo", method = RequestMethod.POST)
    public
    @ResponseBody
    String sendInfo(MultipartHttpServletRequest req){        

       List<MultipartFile> multiPartFileList = req.getFiles("file");
       ...

       String[] json = req.getParameterMap().get("json");

       // Jackson deserialization...but you could use any...Gson, etc
       ObjectMapper mapper = new ObjectMapper();
       TypeReference<Map<String,String>> typeRef = new TypeReference<Map<String,String>>() {};
       Map<String,String> data = mapper.readValue(json[0], typeRef);
       ...

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

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

我认为应该为您做。

HTH

答案 1 :(得分:0)

  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)

答案 2 :(得分:0)

您可以以Object(DTO)的形式发送

像下面一样

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

尝试一下,它应该可以工作

相关问题