【问题标题】:AngularJS - Spring POST request with check boxesAngularJS - 带有复选框的 Spring POST 请求
【发布时间】:2018-12-11 12:46:42
【问题描述】:

我目前正在学习带有 Sprint Boot 和使用 POST 请求的 AngularJS。我知道后端工作正常,但是,我试图弄清楚使用复选框和 Spring Boot MVC 可能会出现什么问题。如果我的代码不好,请原谅,仍然是新的。

错误信息:

{
 "status" : "400",
 "cause" : null,
 "method" : "POST",
 "message" : "Required request body is missing: public void com.velatt.dartentitlements.api.SiteController.addServicesToSite(java.lang.Long,org.springframework.hateoas.Resources<com.velatt.dartentitlements.domain.DeService>) throws java.net.URISyntaxException",
 "exception" : "HttpMessageNotReadableException",
 "path" : "/sites/1/services",
 "error" : "Bad Request"
}

【问题讨论】:

    标签: angularjs spring-mvc spring-boot


    【解决方案1】:

    你必须像下面这样从你的 ajax 传递正文:

            $http({
                method: 'POST',
                url: "/sites/" + $scope.targetEntity.siteId + "/services",
                headers: {'Content-Type': 'application/x-www-form-urlencoded'},
                data:addRequest,
            }).then(function (response) {
                $scope.services = response.data;
            });
    

    【讨论】:

      【解决方案2】:

      试试这样的。

      angularJs

      // create array to hold request data
      var selectedObjArray = [];
      
      // push data to array.
       selectedObjArray.push(....);
      
      
      $http({
              method: 'POST',
              url: "/sites/" + $scope.targetEntity.siteId + "/services",
              headers: {'Content-Type': 'application/json'},   // change content type to json
              data:selectedObjArray ,
          }).then(function (response) {
              $scope.services = response.data;
          });
      

      Spring MVC

      @RequestMapping(value = "/sites/{id}/services", method = RequestMethod.POST, headers = { "Content-type=application/json" })
      public void addServicesToSite(@PathVariable Long id, @RequestBody List<DeService> incoming) throws URISyntaxException {
          for (Link link : incoming.getLinks()) {
              DeService deService = utilityService.getDomainObjectFromUriString(link.getHref(), DeService.class);
              service.addServiceToSite(id, deService);
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2016-01-08
        • 2019-12-26
        • 2020-12-29
        • 1970-01-01
        • 2017-09-28
        • 1970-01-01
        • 2013-08-16
        • 2017-08-24
        相关资源
        最近更新 更多