2017-11-03 2 views
0

난 상태 코드로 NullPointerException GET .각도와 스프링 REST API : 내 백 엔드 (봄)에 게시 한 후 <code>JsonObject</code>로 데이터를 밀어하고 400</p> <p>및 <code>Json</code> 매퍼 <code>(data.get("Field name").asText;</code>를 사용하여 필드를 얻을 : NULL 포인터 예외

는 또한 get(index)data.path하지만 디버그 콘솔로 그것은 JSON 객체가 수신 된 것을 나타냅니다이 반환 null은, 무엇 이러한 필드가 null로 만드는 그 것이다 시도?

이 데이터는 ng-model에서 직접 가져온 것이 아니기 때문에입니까, 아니면 Angular를 사용하여 json 객체에 푸시했기 때문입니까? 400 에로 코드가 표시되기 때문입니다.

MY 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);    
    }); 

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); 


    } 

콘솔 OUTPUT : 미리

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 (작업); 들판을 가져간 후? 매퍼를 사용하지 않고 적어도 하나의 필드를 얻는 방법을 보여줄 수 있습니까? 감사. –

+0

프론트 엔드 (각도)에서 보내는 모든 데이터는 POJO 객체 (OrderOperations)에 매핑됩니다. 정확히 말하면, createOrder (조작)를 호출하여 저장하십시오. 코드를 디버그하면 OrderOperation에 값이 있음을 알 수 있습니다. – mimu1011

+0

그리고 다음과 같은 작업 객체를 반복합니다 : for (I = 0; I <= perations.length(); I ++) {... // save} –

0

을 구문 분석하는 동안 Nullpointer를 얻을 생각합니다. 몇 주 전. - 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;       
    }