我正在使用 AngularJS 向使用 Spring boot 和 Rest 编写的 API 发布一个简单的表单。每个类都用 @RestController 注释
我的弹簧控制器方法如下:
@RequestMapping(method = RequestMethod.POST)
public ResponseEntity<String> create(@Valid @RequestBody Modality modality) {
mService.save(modality);
return ResponseEntity.status(HttpStatus.CREATED).body("");
}
HTML 表单:
<form ng-submit="submit()">
<table>
<tr>
<td>Modality:.</td>
<td><input type="text" name="modality" id="modality" ng-model="Modality.name" value="" /></td>
</tr>
<tr>
<td>Active:.</td>
<td><input type="checkbox" name="active" id="active" ng-model="Modality.active" value="" /></td>
</tr>
<tr>
<td colspan="2">
<button type="submit">Enviar</button>
<input type="reset" value="Reset" />
</td>
</tr>
</table>
</form>
通过 Angular 发布:
$scope.Modality = {id: null, name: '', active: ''}
$scope.submit = function() {
$http({
method: 'POST',
headers : { 'Content-Type': 'application/json'},
url: "http://localhost:8080/v1/modality",
data: $scope.modality
}).success(function(data) {
console.log($scope.modality);
});
}
当我使用角度提交表单时,我可以看到模态对象正在填充表单数据,但是当它到达弹簧控制器时,模态对象的所有属性都为空,因此,我得到以下错误:
{"status":400, "error":"Badequest",
"exception":"org.springframework.http.converter.HttpMessageNotReadableException","message":"Required request body is missing:
public org.springframework.http.ResponseEntity<java.lang.String>
cc.sporthub.controllers.ModalityController.create(cc.sporthub.models.Modality","path":"/v1/modality"}
我究竟做错了什么?