AngularJS:使用$ http.post传递复杂的json数据

Jas*_*ite 1 java json spring-mvc angularjs

我在使用$ http.post在angularjs中传递一个复杂的json对象时遇到了麻烦.我一直收到从服务器发回的400错误请求错误,说请求在语法上是不正确的.我相信它与数组有关,因为当我不包含它时它会很好地传递.

杰森,我正在路过.

{
    customer: {
        firstName: "John",
        lastName: "Doe",
        street: "1234 South Dr",
        city: "Detroit",
        state: "MI",
        zip: "12345",
        phone: "123-321-1234",
        email: "EMAIL@GMAIL.COM"
    },
    order: {
        orderDate: "06-16-2015",
        registerNum: "1",
        transactionNum: "7820",
        deliveryStatusID: 1,
        notes: "Hold order until July",
        items: [
            {skuID: "1234568",
             skuDescription: "Order item 1",
             qty: "4",
             itemStatusID: 1,
             itemStatusDescription: "Backorder"
             },
            {skuID: "7387491",
             skuDescription: "Order item 2",
             qty: "1",
             itemStatusID: 1,
             itemStatusDescription: "Flagged"
            }
        ]
    }
}
Run Code Online (Sandbox Code Playgroud)

角度服务功能

this.addOrder = function(new_order) {
    return $http.post(base + "/add", new_order);
};
Run Code Online (Sandbox Code Playgroud)

Spring MVC控制器方法

@RequestMapping(value="/add", method=RequestMethod.POST)
public void addOrder(@RequestBody CustomerOrder customerOrder) {

    System.out.println("----CUSTOMER-INFO----");
    System.out.println(customerOrder.getCustomer().getFirstName());
    System.out.println(customerOrder.getCustomer().getLastName());

    System.out.println("");
    System.out.println("----ORDER-INFO----");
    System.out.println(customerOrder.getOrder().getOrderID());
    System.out.println(customerOrder.getOrder().getOrderDate());       

}
Run Code Online (Sandbox Code Playgroud)

当我在json中传递items数组时,似乎只会出现这个问题.我没有使用items数组传递相同的json对象,它工作正常.json的格式是以相同的格式发送的,只要我使用angularjs服务方法获取订单就会返回,所以我真的不确定我在哪里出错了.

如果我需要提供更多代码,请告诉我.我很感激帮助我的任何努力.

谢谢.

贾森

Jas*_*ite 7

在努力找到这个问题的错误之后,我终于找到了解决方案.我想我会分享我如何调试和解决这个问题,以防其他人遇到类似我的情况.

在尝试了以角度向服务器发送数据的每种可能方式并不断获得相同的HTTP 400错误之后,我决定将json作为字符串发送并在我的spring mvc控制器中接受json作为字符串.

角度服务方法:

this.addOrder = function(new_order) {
    return $http.post(base + "/add", angular.toJson(new_order));
};
Run Code Online (Sandbox Code Playgroud)

弹簧控制器

@RequestMapping(value="/add", method=RequestMethod.POST, consumes="application/json")
public void addOrder(@RequestBody String json) {

}
Run Code Online (Sandbox Code Playgroud)

从这里我简单地将json传入并使用Jackson ObjectMapper将json字符串转换为我的POJO.

将json字符串映射到pojo

@RequestMapping(value="/add", method=RequestMethod.POST, consumes="application/json")
public void addOrder(@RequestBody String json) {

    ObjectMapper mapper = new ObjectMapper();

    try {

        CustomerOrder order = mapper.readValue(json, CustomerOrder.class);
        System.out.println(order.getCustomer().getFirstName() + " " + order.getCustomer().getLastName());

    } catch (JsonGenerationException e) {

        e.printStackTrace();

    } catch (JsonMappingException e) {

        e.printStackTrace();

    } catch (IOException e) {

        e.printStackTrace();

    }
}
Run Code Online (Sandbox Code Playgroud)

执行此操作并运行我的代码后,在尝试将项json绑定到Order类中的List时,我会在Items类的字段上获得UnrecognizedPropertyException.这只是json方面的一个简单的错误拼写,我完全错过了它.纠正这个后,杰克逊正确地映射了一切,我不再得到这个HTTP 400错误请求在语法上是不正确的.

另外需要注意的是,如果使用JSON.stringify将对象作为字符串传递给角度,则可能会在JSON对象的hashKey字段上遇到此相同的异常.hash使用hashKeys来监视更改.我相信你可以使用jackson注释来忽略未知的字段,或者你可以简单地使用angular.toJson来代替你将删除所有的hasKey /值,这就是我所做的.