【问题标题】:Angular and spring REST API : NULL POINTER EXCEPTIONAngular 和 Spring REST API:空指针异常
【发布时间】: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


    【解决方案1】:

    首先,您不需要将 json 映射到 OrderOperation 对象中。 Spring 会自动将 JSON 对象转换为 OrderOperations,通过参数传递

    public ResponseEntity createOrder(@RequestBody OrderOperations operation)
    

    我认为你在解析数据时会得到 Nullpointer

    【讨论】:

    • @nimu1011 我如何获取字段数据并保存它们?我操作.getTotal() 吗?和 service.createOrder(operation);获得领域后?你能告诉我如何在不使用映射器的情况下获得至少一个字段吗?我以前从未使用过你的方法。谢谢。
    • 您从前端(角度)发送的所有数据都将映射到 POJO 对象(OrderOperations)中。正是你说的,只需调用 createOrder(operation) 来保存它。如果您调试代码,您将看到 OrderOperation 具有值。
    • 我是否循环遍历操作对象,例如:for(I=0; I
    • 如果你从前端发送一个数组。只需在签名中放入一个数组。 public ResponseEntity createOrder(@RequestBody OrderOperations[] operation)
    • 只需调试您的代码,这是正确的解决方案。如果您使用更改代码更新您的问题会很好
    【解决方案2】:

    终于有办法了。几周前。 - 对于那些仍在努力从 JSON 数组中保存多个对象的人 见下文...我希望这对其他人有帮助。

     public List<String> createOrder(@RequestBody OrderOperations[] json) throws OrderOperationsNotFoundException, IOException{
    
                logger.info("recieved json String " + json);            
                List<String> response =  new ArrayList<String>();
                for(OrderOperations op : json){                
                services.createOrder(op); 
                response.add("saved order : " + op.toString());
                }
                return response;                        
         }
    

    【讨论】:

      最近更新 更多