org.springframework.http.converter.HttpMessageNotReadableException:缺少必需的请求正文:

问题描述 投票:0回答:1

我有一个'startDate和'endDate'列表,它通过$ http Post请求传递给控制器​​,但它正在抛出主题中提到的错误。我的代码如下

$scope.saveUpdateAnualAudit=function(auditPlanLst)
    {
        $scope.auditCycleLst = [];
        $scope.auditCycleLst = JSON.stringify(auditPlanLst); 
    //auditPlanLst is a list of startDate and endDate captured from the view
        console.log("$scope.auditCycleLst "+$scope.auditCycleLst);          
        $http({
            method: 'POST',
            url: "PlanAuditController/saveUpdateAnualAudit/"+$scope.auditCycleLst,         

          }).success(function(data, status) {             
          })
            .error(function(data, status) {
                $scope.errorMsg= "<strong>Error!</strong> Failed to retrieve AuditPlan of .";
                $scope.showErrorAlert = true;
                $scope.showSuccessAlert = false;
              });
    }

}]);

控制器代码如下

@RequestMapping(value = "/saveUpdateAnualAudit/{auditCycleLst}", method = RequestMethod.POST) 
    public String saveUpdateAnualAudit(@RequestBody List<AuditCycleJsonVo> auditCycleLst) {     
        try {
            System.out.println("Saved to db");

        }catch(Exception e){
            e.printStackTrace();            
        }
        return "planAudits";    
    }

其中AuditCycleJsonVo类如下所示

public class AuditCycleJsonVo {
    private String startDate;
    private String endDate;

    public String getStartDate() {
        return startDate;
    }
    public void setStartDate(String startDate) {
        this.startDate = startDate;
    }
    public String getEndDate() {
        return endDate;
    }
    public void setEndDate(String endDate) {
        this.endDate = endDate;
    }

}

我无法通过它抛出的错误找到它出错的地方,它不会出现在控制器上。

angularjs spring-4
1个回答
0
投票

尝试将auditCycleList作为RequestBody参数发送,因为控制器期望:

您可以像这样构建请求:

var req = {
   method: 'POST',
   url: 'PlanAuditController/saveUpdateAnualAudit',
   headers: {
     'Content-Type': "application/json"
   },
   data: $scope.auditCycleLst
}

$http(req).success(function(){...}).error(function(){...});
© www.soinside.com 2019 - 2024. All rights reserved.