簡體   English   中英

在Spring Boot應用程序中將來自angularjs的json發送到spring控制器時導致異常

[英]causing exception while sending json from angularjs to spring controller in spring boot application

我正在嘗試使用angularjs生成動態表行,並且工作正常,但問題是在將json發送到服務器(即spring boot controller)時,它引起了如下異常:

nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of com.practice.dto.BillsDto out of START_ARRAY token

json是這樣的:

[
  {
    "description": "sfd",
    "quantity": 3,
    "amount": 2
  },
  {
    "description": "sdf",
    "quantity": 4,
    "amount": 2
  }
]

dto是

public class BillsDto {
    private List<BillDto> billDtos;

    public BillsDto() {

    }

   // getters setters
}

另一個dto是:

 public class BillDto {
        private int id;
        private String description;
        private double quantity;
        private double amount;

        public BillDto() {

        }

        // getters setters
    }

彈簧控制器的方法是:

@RequestMapping(value = "/savePatientBill", method = RequestMethod.POST)
    public ModelAndView saveBill(@RequestBody BillsDto billings){
        System.out.println(billings.getBillDtos().size());
        for(BillDto billDto: billings.getBillDtos()){
            System.out.println(billDto.getDescription());
            System.out.println(billDto.getAmount());
            System.out.println(billDto.getQuantity());
          }
        }

angularjs代碼是:

app.controller('newBillCtrl', function ($scope, $http, $httpParamSerializer, PatientConstants) {
    $scope.billings = [{}];

    $scope.addNew = function (billings) {
        $scope.billings.push({
            'description': "",
            'quantity': "",
            'amount': "",
        });
    };

    $scope.remove = function () {
        var newDataList = [];
        $scope.selectedAll = false;
        angular.forEach($scope.billings, function (selected) {
            if (!selected.selected) {
                newDataList.push(selected);
            }
        });
        $scope.billings = newDataList;
    };

    $scope.checkAll = function () {
        if (!$scope.selectedAll) {
            $scope.selectedAll = true;
        } else {
            $scope.selectedAll = false;
        }
        angular.forEach($scope.billings, function (billings) {
            billings.selected = $scope.selectedAll;
        });
    };
    $scope.submit = function () {
        $scope.bill=angular.toJson($scope.billings);
        console.log($scope.bill)
        $http.post(PatientConstants.DOMAIN_URL + '/savePatientBill', $scope.bill);
    }

});

而我的html模板是:

<script type="text/ng-template" id="newBill.jsp">
    <div class="container-fluid">
        <div class="row">
            <div style="margin-bottom: 50px;">
                <h2 align="center">Patient New Bill</h2>
            </div>
            <div class="container">
                <div class="row">
                    <div class="col-md-12">
                        <div class="panel panel-default">
                            <div class="panel-body">
                                <form ng-submit="submit()">
                                    <table class="table table-striped table-bordered">
                                        <thead>
                                        <tr>
                                            <th><input type="checkbox" ng-model="selectedAll" ng-click="checkAll()" /></th>
                                            <th>Description</th>
                                            <th>Quantity</th>
                                            <th>Amount</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        <tr ng-repeat="billing in billings">
                                            <td>
                                                <input type="checkbox" ng-model="billing.selected"/></td>
                                            <td>
                                                <input type="text" class="form-control" ng-model="billing.description" required/></td>
                                            <td>
                                                <input type="number" class="form-control" ng-model="billing.quantity" required/></td>
                                            <td>
                                                <input type="number" class="form-control" ng-model="billing.amount" required/></td>
                                        </tr>
                                        </tbody>
                                    </table>

                                    <div class="form-group">
                                        <input ng-hide="!billings.length" type="button" class="btn btn-danger pull-right" ng-click="remove()" value="Remove">
                                        <input type="button" ng-click="addNew()" class="btn btn-primary addnew pull-right" value="Add New">
                                        <input type="submit" class="btn btn-primary addnew pull-right" value="Submit">
                                    </div>
                                </form>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</script>

請幫助我。感謝您的協助。

您對REST服務的json請求應如下所示。 請在soapui或Postman中測試您的服務,並驗證jsp / html頁面生成的請求。

{
"billDtos" : [
 {
   "description": "sfd",
   "quantity": 3,
   "amount": 2
 },
 {
   "description": "sdf",
   "quantity": 4,
   "amount": 2
  }
 ]
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM