简体   繁体   English

当参数作为RequestBody和RequestParam发送时,sprig控制器无法识别所发送的请求

[英]the request sent is not recognized by the sprig controller when parameters are sent as RequestBody and RequestParam

I am working on spring application with angularjs. 我正在使用angularjs进行Spring应用程序。 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. 我正在尝试将JSON对象作为RequestBody和RequestParam信息从angularjs发送到spring控制器,但是spring控制器无法识别从angularjs发送的请求。

js: 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. PS:弹簧控制器的路径是正确的,因为当我仅将一个参数从angularjs传递给弹簧控制器时,它将触及弹簧控制器。 Sample code below: 下面的示例代码:

js: 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: 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. ----编辑---我尝试按照以下2种方式将RequestBody更改为RequestParam,但是两种方式均未触及spring控制器。

First trial: js: 第一次试用: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: 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 -编辑----我在servlet.xml中配置了CommonsMultipartResolver

<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. 在服务器中,如果您以JSON形式发送数据,则将headers = {"content-type=multipart/form-data"}更改为headers = {"content-type=application/json"}然后检查是否启用了CORS。 Change your headers from front end headers: {'Content-Type': undefined} to headers: {'Content-Type': "content-type=multipart/form-data"} 将您的标头从前端标头:{'Content-Type':undefined}更改为标头:{'Content-Type':“ content-type = multipart / form-data”}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Java / Spring&gt;在请求中未发送任何正文时,使用@RequestBody处理控制器方法的错误请求响应 - Java/Spring > Handle Bad Request response for controller method with @RequestBody when no body is sent in request 客户端发送的请求在语法上是不正确的。 在春天使用@RequestParam - The request sent by the client was syntactically incorrect. using @RequestParam in spring 无法读取控制器中RedirectAttribute发送的参数 - cannot read parameters sent by RedirectAttribute in controller 在gwt-upload中将请求参数发送到UploadAction - Sent request parameters to UploadAction in gwt-upload @RequestParam不接受从请求发送的字符串中的“&”,在&之前存储所有字符,并截断其余字符 - @RequestParam not accepting '&' in a string sent from request, stores all characters before & and truncates the rest Spring Controller无法通过RequestBody或RequestParam接收文件 - Spring Controller does not receive file through RequestBody or RequestParam 通过curl发送发布请求时,java Spring Boot获取控制器中的文件名 - java Spring boot get the file name in the controller when post request is sent via curl 客户端发送的请求在语法上不正确,尝试绑定数据并将其发送到控制器时发生错误 - The request sent by the client was syntactically incorrect error occurs when trying to bind the data and send to controller @RequestBody 和 @RequestParam 的 RestTemplate - RestTemplate for @RequestBody and @RequestParam 通过HttpPut请求发送的查询参数未正确读取 - Query parameters sent with HttpPut Request not being read properly
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM