0

I'm trying to call a web service implementyed by Spring REST but the server respond with 400 http code bad request.I will expose some informations about that Ihope that someone can help me.

This is my controller method:

@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";

}

The manner by that I call the web service: $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.
    });

};

    };

the tow objects "userr" and "postt":

$scope.userr = {
        userId : 15,
        firstName : "foulen",
        lastName: "ben foulen"};

    $scope.postt = {
            Id : "18",
            datePost : null,
            note: "Mon message"};

The response of the server:

Etat HTTP 400 - Required PostDto parameter 'post' is not present

these are the headers of request and reponse:

  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
housseminfo
  • 97
  • 4
  • 12

3 Answers3

0

The parameters whichever you are passing , are they getting passed to the controller ? I mean to the url you mentioned in the angular controller 'http://localhost:8080/wall/addpost'.

Shweta M
  • 146
  • 1
  • 2
  • 11
0

your Angular codes seems good. I think that the problem is on the side of the server codes. The request parameter @RequestParam PostDto post should have a value to indicate its name, like this : @RequestParam PostDto("post") post. According to spring doc, this value by default is "".

To complete, maybe you should use a more normal way of sending request of Angular. I mean to use the json object for the parameters, but not the html form. Because you have already your models of user and post.

Hope help.

EDIT : Can you modify your angular codes, to send the parameter by json object.

$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.
    });

};

And delete the header in your java codes :

@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";

}

EDIT : model:

$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...
}
Qianyue
  • 1,767
  • 19
  • 24
  • the same problem persist even after I rename the params – housseminfo Feb 27 '15 at 10:00
  • @housseminfo I have updated my answer, to use json object as request parameter. – Qianyue Feb 27 '15 at 10:14
  • i get the same response by the server "Etat HTTP 400 - Required PostDto parameter 'post' is not present" – housseminfo Feb 27 '15 at 10:20
  • @housseminfo, err, can you have a look at the header of your request in the console of Chrome or Firefox ? To check if the parameters are sent to the server. – Qianyue Feb 27 '15 at 10:25
  • @housseminfo, I've noticed that, since you have changed to the json object, you should use the annotation @RequestBody, just like `@RequestBody PostDto post`. – Qianyue Feb 27 '15 at 11:17
  • I'd already tested it but the same problem persist. Look at my updated question – housseminfo Feb 27 '15 at 11:28
  • @housseminfo, as Shweta M says, you should wrapper your model, just like what I updated this answer. – Qianyue Feb 27 '15 at 12:09
0

You cannot use two @RequestBody as it can bind to a single object only (the body can be consumed only once).the much easier way would be to introduce a wrapper Object and change your signature.

Shweta M
  • 146
  • 1
  • 2
  • 11