我正在尝试调用由Spring REST实现的Web服务,但服务器响应400 http代码错误请求。我将公开一些有关Ihope的信息,有人可以帮助我。
这是我的控制器方法:
@RequestMapping(value = "/addpost", method = RequestMethod.POST,headers = "Accept=application/json")
public @ResponseBody
String addpost(@RequestBody PostDto post, @RequestBody UserDto user) {
postservice.addPost(post, user);
return "post inserted";
}
我称之为Web服务的方式: $ scope.addPost = function(){
$scope.addPost = function() {
$http({
method : 'POST',
url : 'http://localhost:8080/wall/addpost',
data : ({
post : $scope.postt,
user : $scope.userr
}),
}).success(function(data, status, headers, config) {
// $scope.persons.push(data);
alert("Success");
$scope.user = "";
$scope.post = "";
}).error(function(data, status, headers, config) {
alert("erreur");
// called asynchronously if an error occurs
// or server returns response with an error status.
});
};
};
两个对象“userr”和“postt”:
$scope.userr = {
userId : 15,
firstName : "foulen",
lastName: "ben foulen"};
$scope.postt = {
Id : "18",
datePost : null,
note: "Mon message"};
服务器的响应:
Etat HTTP 400 - Required PostDto parameter 'post' is not present
这些是请求和响应的标题:
Remote Address:127.0.0.1:8080
Request URL:http://localhost:8080/wall/addpost
Request Method:POST
Status Code:400 Mauvaise Requête
Request Headersview source
Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate
Accept-Language:fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4
Connection:keep-alive
Content-Length:125
Content-Type:application/json;charset=UTF-8
Host:localhost:8080
Origin:http://localhost:8080
Referer:http://localhost:8080/wall/view/test.html
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.115 Safari/537.36
Request Payloadview source
{post: {idpost: 18, datePost: null, note: "Mon message"},…}
post: {idpost: 18, datePost: null, note: "Mon message"}
datePost: null
idpost: 18
note: "Mon message"
user: {userId: 15, firstName: "foulen", lastName: "ben foulen"}
firstName: "foulen"
lastName: "ben foulen"
userId: 15
Response Headersview source
Connection:close
Content-Length:983
Content-Type:text/html;charset=utf-8
Date:Fri, 27 Feb 2015 11:16:49 GMT
Server:Apache-Coyote/1.1
答案 0 :(得分:0)
您传递的参数是否会被传递给控制器?我的意思是你在角度控制器中提到的url 'http://localhost:8080/wall/addpost'。
答案 1 :(得分:0)
你的Angular代码似乎很好。我认为问题出在服务器代码的一边。请求参数@RequestParam PostDto post
应该有一个值来指示其名称,如下所示:@RequestParam PostDto("post") post
。根据{{3}},此值默认为""。
要完成,也许您应该使用更正常的方式发送Angular的请求。我的意思是使用json对象作为参数,而不是html表单。因为您已经拥有用户和帖子的模型。
希望帮助。
编辑: 你可以修改你的角度代码,通过json对象发送参数。
$http({
method : 'POST',
url : 'http://localhost:8080/wall/addpost',
data : ({
post : $scope.postt,
user : $scope.userr
})
}).success(function(data, status, headers, config) {
// $scope.persons.push(data);
alert("Success");
$scope.user = "";
$scope.post = "";
}).error(function(data, status, headers, config) {
alert("erreur");
// called asynchronously if an error occurs
// or server returns response with an error status.
});
};
删除java代码中的标题:
@RequestMapping(value = "/addpost", method = RequestMethod.POST)
public @ResponseBody
String addpost(@RequestParam(value="post") PostDto post, @RequestParam(value="user") UserDto user) {
postservice.addPost(post, user);
return "post inserted";
}
编辑: 型号:
$scope.postInfo= {
userId : 15,
firstName : "foulen",
lastName: "ben foulen",
postId : "18",
datePost : null,
postnote: "Mon message"
};
AJAX:
$http({
method : 'POST',
url : 'http://localhost:8080/wall/addpost',
data : ({
post : $scope.postInfo
}).// your other codes
})
JAVA:
@RequestMapping(value = "/addpost", method = RequestMethod.POST)
public @ResponseBody String addpost(@RequestBody PostInfoDto post) {
// codes...
}
答案 2 :(得分:0)
你不能使用两个@RequestBody,因为它只能绑定到一个对象(主体只能被消耗一次)。更简单的方法是引入一个包装器对象并更改你的签名。