我目前正在通过Sprint Boot学习AngularJS,并使用POST请求。我知道后端工作正常,但是,我试图弄清楚使用复选框和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"
}
答案 0 :(得分: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;
});
答案 1 :(得分:0)
尝试这样的事情。
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);
}
}