【问题标题】:org.springframework.http.converter.HttpMessageNotReadableException: Required request body is missing:org.springframework.http.converter.HttpMessageNotReadableException:缺少所需的请求正文:
【发布时间】:2018-01-04 20:25:07
【问题描述】:

我有一个 '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】:

    尝试将 auditCycleList 作为 RequestBody 参数发送,正如控制器所期望的那样:

    您可以像这样构建请求:

    var req = {
       method: 'POST',
       url: 'PlanAuditController/saveUpdateAnualAudit',
       headers: {
         'Content-Type': "application/json"
       },
       data: $scope.auditCycleLst
    }
    
    $http(req).success(function(){...}).error(function(){...});
    

    【讨论】:

      猜你喜欢
      • 2019-09-09
      • 2017-12-02
      • 1970-01-01
      • 1970-01-01
      • 2016-06-03
      • 1970-01-01
      • 2017-08-01
      • 2019-10-01
      • 1970-01-01
      相关资源
      最近更新 更多