使用spring和AngularJS发布数据时出现问题::客户端发送的请求在语法上是不正确的

时间:2014-04-08 16:33:53

标签: java spring angularjs rest spring-mvc

我在POSTING到休息服务时遇到错误。请帮我理解我在这里缺少的东西?

The request sent by the client was syntactically incorrect.

我的控制器代码是:

@RequestMapping(method = RequestMethod.POST)
public @ResponseBody String addUser(@RequestBody UserDomain userDomain, BindingResult result) {
    System.out.println("UserDomain :: " + userDomain);
    userService.addUser(userDomain);
    return "redirect:/";
}

Services.js

services.factory('UserFactory', ['$resource', function ($resource) {

return  {
    usercreation: $resource('/ngdemo/web/users', {}, {
        query: {method: 'GET', isArray: true },
        create: {method: 'POST'}
    })

}; }]);

Controller.js

    app.controller('DemoCtrl', ['$scope', 'UserFactory', '$facebook', 'NotificationFactory', '$location', function ($scope, UserFactory, $facebook, NotificationFactory, $location) {

        $scope.UserDomain = { firstname: null };

        $scope.isLoggedIn = false;
        $scope.login = function () {
            $facebook.login().then(function () {
                refresh();
            });
        }

        function refresh() {
            $facebook.api("/me").then(
                function (response, Userfactory) {
                    $scope.welcomeMsg = "Welcome " + response.name;
                    $scope.result = response;
                    console.log("console ctrl demo12 - " + $scope.welcomeMsg);
                    alert($scope.result);
                    $scope.isLoggedIn = true;

                    //$scope.UserDomain.id = $scope.result.id;
                    $scope.UserDomain.socialId = $scope.result.id;
                    $scope.UserDomain.gender = $scope.result.gender;
                    $scope.UserDomain.firstname = $scope.result.first_name;
                    $scope.UserDomain.hometown = $scope.result.hometown.name;
                    $scope.UserDomain.currentLocation = $scope.result.location.name;
                    $scope.UserDomain.lastname = $scope.result.last_name;
                    $scope.UserDomain.email = $scope.result.email;
                    $scope.UserDomain.username = $scope.result.username;
                    $scope.UserDomain.link = $scope.result.link;
                    $scope.UserDomain.relationshipStatus = $scope.result.relationship_status;

                    console.log("$scope.result - " + $scope.result.relationship_status);
                    console.log("$scope.UserDomain - " + $scope.UserDomain.relationshipStatus);

                    //making call to add user
                    UserFactory.usercreation.create($scope.UserDomain);
                },
                function (err) {
                    $scope.welcomeMsg = "Please log in";
                });
        }
    }]);

在mozilla控制台中编写和检查JSON ::

{"firstname":"Naveuuuin","socialId":"7048797897728972","gender":"male","hometown":"Udaipur, Rajasthan","currentLocation":"Bangalore, India","lastname":"Vyas","email":"vyas@gmail.com","username":"vyas","link":"https://www.facebook.com/vyas","relationshipStatus":"Single"}

UserDomain类是:

    package ngdemo.domain;

    import javax.persistence.*;

    @Entity
    @Table(name = "P_USER")
    public class UserDomain {

        @Id
        @Column(name = "USER_ID")
        @GeneratedValue
        private Integer id;

        @Column(name = "SOCIAL_ID")
        private String socialId;

        @Column(name = "FIRSTNAME")
        private String firstname;

        @Column(name = "LASTNAME")
        private String lastname;

        @Column(name = "EMAIL")
        private String email;

       /* @Column(name = "PHONENUMBER")
        private String telephone;*/

        @Column(name = "HOMETOWN")
        private String homeTown;

        @Column(name = "GENDER")
        private String gender;

        @Column(name = "LINK")
        private String link;

        @Column(name = "CURRENT_LOCATION")
        private String currentLocation;

        @Column(name = "RELATIONSHIP_STATUS")
        private String relationshipStatus;

        @Column(name = "USERNAME")
        private String username;

        public String getSocialId() {
            return socialId;
        }

        public void setSocialId(String socialId) {
            this.socialId = socialId;
        }

        public String getCurrentLocation() {
            return currentLocation;
        }

        public void setCurrentLocation(String currentLocation) {
            this.currentLocation = currentLocation;
        }

        public String getGender() {
            return gender;
        }

        public void setGender(String gender) {
            this.gender = gender;
        }

        public String getHomeTown() {
            return homeTown;
        }

        public void setHomeTown(String homeTown) {
            this.homeTown = homeTown;
        }

        public String getLink() {
            return link;
        }

        public void setLink(String link) {
            this.link = link;
        }

        public String getRelationshipStatus() {
            return relationshipStatus;
        }

        public void setRelationshipStatus(String relationshipStatus) {
            this.relationshipStatus = relationshipStatus;
        }

        public String getUsername() {
            return username;
        }

        public void setUsername(String username) {
            this.username = username;
        }

        public String getEmail() {
            return email;
        }

       /* public String getTelephone() {
            return telephone;
        }*/

        public void setEmail(String email) {
            this.email = email;
        }

       /* public void setTelephone(String telephone) {
            this.telephone = telephone;
        }*/

        public String getFirstname() {
            return firstname;
        }

        public String getLastname() {
            return lastname;
        }

        public void setFirstname(String firstname) {
            this.firstname = firstname;
        }

        public void setLastname(String lastname) {
            this.lastname = lastname;
        }

        public Integer getId() {
            return id;
        }

        public void setId(Integer id) {
            this.id = id;
        }

        @Override
        public String toString() {
            return "UserDomain{" +
                    "currentLocation='" + currentLocation + '\'' +
                    ", id=" + id +
                    ", socialId=" + socialId +
                    ", firstname='" + firstname + '\'' +
                    ", lastname='" + lastname + '\'' +
                    ", email='" + email + '\'' +
                    ", homeTown='" + homeTown + '\'' +
                    ", gender='" + gender + '\'' +
                    ", link='" + link + '\'' +
                    ", relationshipStatus='" + relationshipStatus + '\'' +
                    ", username='" + username + '\'' +
                    '}';
        }
    }

1 个答案:

答案 0 :(得分:0)

非常感谢所有人!

一切都是正确的,唯一的问题是我的JSON返回的套管和域模型。