Java AngularJS Spring MVC - $ http.post 400错误请求在将bean发送到控制器时语法错误

时间:2016-07-15 07:47:45

标签: java angularjs spring spring-mvc

我正在编写一个应用程序,该应用程序将假定一个管理员将使用他/她的详细信息创建一个员工记录,并为新人分配一个现有的"团队"以及与他/她具有或要求的角色/位置相对应的技能组合。当然,这将有4个不同的对象来处理它。我决定使用bean来存储Employee对象,Team对象以及SkillsRoles对象。

第一个bean将存储SkillsRolesRoleNSkills。每个Role可能需要多个Skills,而Employee可能需要多个Roles。每个Employee一次只能分配给一个Team。因此,第二个bean将采用RoleNSkills列表以及employee数据和他将被分配到的team

注意,teamgroup在此上下文中的含义相同。我们使用team作为前端目的。

错误:

enter image description here

这是谷歌浏览器返回给我的对象,而我正在试图找出究竟是哪个资源导致我出现问题。我之前已将employee添加为单个资源,但它的工作原理将被排除在外。

Object returned

JSON格式化程序:http://www.freeformatter.com/json-formatter.html 下面是我设法从我正在处理的对象中获取的全部JSON:

{
   "employee": {
      "firstName": "234243",
      "middleName": "234234",
      "lastName": "234234",
      "alias": "234234",
      "dob": "2016-07-07T16:00:00.000Z",
      "mobile": "234",
      "email": "234234@24234324",
      "homeNumber": "12312312",
      "nationality": "Malaysia",
      "nric": "123213123",
      "resident": "Singapore Citizen",
      "address": "123123123"
   },
   "roleNSkills": [
      {
         "role": {
            "id": 9,
            "title": "Excavator Operator",
            "status": 1,
            "owner": 0,
            "createdAt": 1456804702000,
            "updatedAt": 1456804702000,
            "rawSkills": [
               {
                  "id": 1,
                  "title": "Drive Excavator",
                  "description": "People who can drive and operate an excavator.",
                  "status": 1,
                  "owner": 0,
                  "createdAt": 1456800101000,
                  "updatedAt": 1456800101000
               },
               {
                  "id": 2,
                  "title": "English Competency",
                  "description": "English speaking ability.",
                  "status": 1,
                  "owner": 0,
                  "createdAt": 1456800303000,
                  "updatedAt": 1456800303000
               }
            ]
         },
         "skills": [
            {
               "id": 1,
               "title": "Drive Excavator",
               "description": "People who can drive and operate an excavator.",
               "status": 1,
               "owner": 0,
               "createdAt": 1456800101000,
               "updatedAt": 1456800101000,
               "$$hashKey": "object:167",
               "certification": "12312",
               "certificated": true
            },
            {
               "id": 2,
               "title": "English Competency",
               "description": "English speaking ability.",
               "status": 1,
               "owner": 0,
               "createdAt": 1456800303000,
               "updatedAt": 1456800303000,
               "$$hashKey": "object:168",
               "certification": "213123",
               "certificated": true
            }
         ],
         "$$hashKey": "object:184"
      },
      {
         "role": {
            "id": 10,
            "title": "Admin",
            "status": 1,
            "owner": 0,
            "createdAt": 1456819064000,
            "updatedAt": 1456819064000,
            "rawSkills": [
               {
                  "id": 1,
                  "title": "Drive Excavator",
                  "description": "People who can drive and operate an excavator.",
                  "status": 1,
                  "owner": 0,
                  "createdAt": 1456800101000,
                  "updatedAt": 1456800101000
               },
               {
                  "id": 2,
                  "title": "English Competency",
                  "description": "English speaking ability.",
                  "status": 1,
                  "owner": 0,
                  "createdAt": 1456800303000,
                  "updatedAt": 1456800303000
               },
               {
                  "id": 3,
                  "title": "Drive Recovery Vehicle",
                  "description": "6 axis long vehicle",
                  "status": 1,
                  "owner": 0,
                  "createdAt": 1456800410000,
                  "updatedAt": 1456800410000
               },
               {
                  "id": 4,
                  "title": "Japanese Compentency",
                  "description": "Japanese speaking ability.",
                  "status": 1,
                  "owner": 0,
                  "createdAt": 1456800481000,
                  "updatedAt": 1456800481000
               }
            ]
         },
         "skills": [
            {
               "id": 1,
               "title": "Drive Excavator",
               "description": "People who can drive and operate an excavator.",
               "status": 1,
               "owner": 0,
               "createdAt": 1456800101000,
               "updatedAt": 1456800101000,
               "$$hashKey": "object:196",
               "certification": "123123",
               "certificated": true
            },
            {
               "id": 2,
               "title": "English Competency",
               "description": "English speaking ability.",
               "status": 1,
               "owner": 0,
               "createdAt": 1456800303000,
               "updatedAt": 1456800303000,
               "$$hashKey": "object:197",
               "certification": "213123",
               "certificated": true
            },
            {
               "id": 3,
               "title": "Drive Recovery Vehicle",
               "description": "6 axis long vehicle",
               "status": 1,
               "owner": 0,
               "createdAt": 1456800410000,
               "updatedAt": 1456800410000,
               "$$hashKey": "object:198",
               "certification": "3123123",
               "certificated": true
            },
            {
               "id": 4,
               "title": "Japanese Compentency",
               "description": "Japanese speaking ability.",
               "status": 1,
               "owner": 0,
               "createdAt": 1456800481000,
               "updatedAt": 1456800481000,
               "$$hashKey": "object:199",
               "certification": "123123",
               "certificated": true
            }
         ],
         "$$hashKey": "object:228"
      }
   ],
   "rawGroupToEmployee": {
      "groupId": 89
   }
}

这是我的豆子: Bean

请求的bean: RawEmployee:Raw Employee

技能和角色bean:Skills and roles

RawGroupToEmployee beans:Group

控制器方法和路由:

@Controller
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    //adding paramEmployee bean
    @ResponseBody
    @RequestMapping(value="/add", method = RequestMethod.POST)
    public Result addEmployee(@RequestBody ParamEmployee paramEmployee)
    {
        //employeeService.addEmployee(paramEmployee);
        return ResultFactory.ok("Employee inserted.");
        //return paramEmployee;
    }

}

AngularJS发布帖子

$scope.insertEmployee = function () {
        //loading into param employee bean
        $scope.rawGroupToEmployee =$scope.rawGroup;
        var paramEmployee = {
            employee: $scope.rawEmployee,
            roleNSkills: $scope.selectedRoles,
            rawGroupToEmployee: $scope.rawGroupToEmployee
        };
        console.log(paramEmployee);

        //$http.post("/employee/create", $scope.rawEmployee).success(function (resp) {
        $http.post("/employee/add", paramEmployee).success(function (resp) { //posting bean instead of raw employee this time.
            //console.log(JSON.stringify($scope.rawEmployee));
            //$scope.rawEmployee = resp;
            console.log(resp);
        });
    };

更新:仍然没有。我怀疑它是因为RoleNSkills下的CreatedAt和UpdatedAt是以毫秒为单位而且我试图将这些发布到控制器并完全拒绝我。但我试图找到那些被定义的地方,但无济于事。如果有人愿意帮助我搜索,我可以发布任何请求的资源供您查看。

Update2:也许,因为在我的数据库中,它以这种方式定义'2015-06-22 15:03:09',当我发出" get"从数据库中获取所有技能和角色,它以毫秒的格式给我,当我发布时我应该这样做吗?

1 个答案:

答案 0 :(得分:2)

您获得的错误与发送到控制器的错误json有关,很可能通过使用将{Javascript对象序列化为字符串的JSON.stringify(paramEmployee)来解决此问题。