2017-11-03 10 views
0

私は、ステータスコードでNullPointerExceptionをGET:400角度と春のREST API:NULLポインタ例外

私はJsonObjectにデータをプッシュして、私のバックエンド(春)に投稿してJsonマッパー(data.get("Field name").asText;を使用してフィールドを取得しています。

それはそれは、これらのフィールドはNULLになることです何か、JSONオブジェクトが受信されたことを示すデバッグコンソールのように、私もどこget(index)data.pathが、この戻りnullを、試してみましたか?

このデータはng-modelから直接取得されたものではありませんか、それとも、私が400エラーコードを取得しているため、Angularを使用してjsonオブジェクトにプッシュしたからですか?

MYアンギュラ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);    
    }); 

MY SPRINGコントローラUPDATE

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"}] 

おかげ

答えて

0

まず、jsonをOrderOperationオブジェクトにマップする必要はありません。 春は自動的にパラメータ

public ResponseEntity createOrder(@RequestBody OrderOperations operation) 

として渡すことで、OrderOperationsにJSONオブジェクトに変換しますし、私はあなたが最後に、私は解決策を持っているデータ

+0

@ nimu1011フィールドデータを取得して保存するにはどうすればよいですか? operation.getTotal()を実行しますか?およびservice.createOrder(operation);フィールドを取得した後に?マッパーを使わずに少なくとも1つのフィールドを取得する方法を私に示してもらえますか?ありがとう。 –

+0

フロントエンド(角度)から送信されるすべてのデータは、POJOオブジェクト(OrderOperations)にマップされます。正確には、createOrder(操作)を呼び出して保存するだけです。コードをデバッグすると、OrderOperationに値があることがわかります。 – mimu1011

+0

そして、私は操作オブジェクトのようにループしますか:for(I = 0; I <= perations.length(); I ++){... // save} –

0

を解析しながら、あなたはnullポインタを取得すると思います。数週間前。 - 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;       
    }