vendredi 27 février 2015

calling Spring Rest method with AngularJs

while trying to call web service implemented with spring REST I get tyhe 400 code state http request error. I will expose some code hope find someone that can help me.


My controller method:



@RequestMapping(value = "/addpost", method = RequestMethod.POST)
public @ResponseBody
String addpost(@RequestBody WrapperPostUser postuser) {

PostDto post = postuser.getPost();
UserDto user = postuser.getUser();
postservice.addPost(post, user);
return "post inserted";


The class WrapperPostUser:



public class WrapperPostUser implements Serializable {

private PostDto post;
private UserDto user;

//getters and setters }


Finally, calling the method with AngularJs:



$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 Server Response: 400 http : Bad Request


the tow objects:



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

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

Aucun commentaire:

Enregistrer un commentaire