【发布时间】:2025-12-03 22:15:01
【问题描述】:
我 GET NullPointerException 状态码:400
我将数据推送到JsonObject,然后将其发布到我的后端(Spring)并使用Json映射器(data.get("Field name").asText;获取字段。
我也试过get(index)和data.path,但是这个返回null,在调试控制台中表示接收到Json对象,是什么让这些字段为空?
是因为这些数据不是直接来自 ng-model,还是因为我使用 Angular 将它推送到 json 对象中,因为我得到了 400 个错误代码。
我的 Angular JS:
app.controller("OrderOperationsCtrl", function($scope, $http,$window) {
$http.get(productURL).then(function(response) {
$scope.productData = response.data;
$scope.newData = [];
$scope.total = 0.0;
$scope.sync = function(bool,productData,submit){
if(bool === true){
var numberOrdered = 0;
numberOrdered++;
$scope.total = $scope.total + productData.price;
var newD = $scope.newData.push({
"numberOrdered":numberOrdered,
"customerId":localStorage.getItem('takealot_customer'),
"id":productData.id,
"total":$scope.total
});
$scope.sender = $scope.newData;
console.log(" NewD " + $scope.newData);
}
if(submit === true){
$http.put(orderURL,JSON.stringify($scope.sender)).then(function(response){
if(response.data)
console.log("Order created !");
},function(response){
console.log("Error creating Order !");
console.log(response.status);
});
我的弹簧控制器更新
public ResponseEntity createOrder(@RequestBody OrderOperations[] operation) throws OrderOperationsNotFoundException, IOException{
logger.info("recieved json String " + operation);
services.createOrder(operation);
return new ResponseEntity(HttpStatus.OK);
}
控制台输出:
Hibernate: select products0_.id as id1_3_, products0_.category as category2_3_, products0_.description as descript3_3_, products0_.name as name4_3_, products0_.price as price5_3_, products0_.quantity as quantity6_3_, products0_.supplier as supplier7_3_ from products products0_
2017-11-03 11:34:41.689 INFO 10504 --- [nio-8080-exec-9]
c.p.C.OrderOperationsController : recieved json String
[{"numberOrdered":1,"customerId":null,"id":1,"total":78.32,"$$hashKey":"object:17"},
{"numberOrdered":1,"customerId":null,"id":2,"total":156.70999999999998,"$$hashKey":"object:19"},
{"numberOrdered":1,"customerId":null,"id":1,"total":235.02999999999997,"$$hashKey":"object:21"}]
提前谢谢
【问题讨论】:
标签: angularjs json spring-mvc spring-boot maven-2