【问题标题】:causing exception while sending json from angularjs to spring controller in spring boot application在spring boot应用程序中将json从angularjs发送到spring控制器时导致异常
【发布时间】:2016-08-30 02:59:59
【问题描述】:

我正在尝试使用 angularjs 生成动态表行,它工作正常,但问题是在将 json 发送到服务器(即 spring boot 控制器)时,它会导致如下异常:

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
    }

spring 控制器方法是:

@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>

请帮助我。谢谢。

【问题讨论】:

    标签: java angularjs json spring spring-boot


    【解决方案1】:

    您对 REST 服务的 json 请求应如下所示。请在soapui或Postman中测试您的服务,并验证您的jsp/html页面生成的请求。

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

    【讨论】:

    • 感谢您的回复。您提供的 json 在邮递员中工作正常,但我提到的 json 无法正常工作。我是 angularjs 的初学者。所以请您在 angularjs 中指导我,如何将我的json转换成你提到的json
    • 感谢问题已解决。我把我的json格式转成你提到的json格式了
    猜你喜欢
    • 2015-02-19
    • 1970-01-01
    • 2023-01-19
    • 2021-06-27
    • 2012-09-09
    • 2016-09-21
    • 2018-12-06
    • 2012-01-29
    • 2018-01-18
    相关资源
    最近更新 更多