java - 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. 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.
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. Sample code below:
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:
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.
First trial: 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:
..
$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
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="10000000"/>
</bean>
解决方案
在服务器中,如果您以 JSON 的形式发送数据,则更headers = {"content-type=multipart/form-data"}
改为 headers = {"content-type=application/json"}
并检查是否启用了 CORS。将您的标题从前端标题:{'Content-Type': undefined} 更改为标题:{'Content-Type': "content-type=multipart/form-data"}
推荐阅读
- python - ML Engine:将 GCS 文件导入机器的最简单方法
- uwp - 使用对象调用时未找到 UIElement 的成员
- android - 如何确定Android崩溃的原因?
- c - 堆栈上的可变大小数组
- java - PHP 中的 HMAC 输出与 Java 中的输出不匹配
- python - 正确的 python 正则表达式返回 NoneType
- react-native - React Native 自定义组件重用与动态函数
- visual-studio - Reference GAC assemblies in new CSPROJ format?
- firebase - 每当我对集合中的文档进行更新时,Firestore 不会返回所有元素,而是仅返回更新的元素
- symfony - Mautic Contact Field Api 请求返回 400 作为响应