AngularJS 和 Spring 休息
AngularJS and Spring Rest
我正在使用 AngularJS 来 post 一个用 Spring 引导和休息编写的 API 的简单形式。每个 class 都用 @RestController
注释
我的spring控制器方法如下:
@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>
Post 通过 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);
});
}
当我使用 angular 提交表单时,我可以看到模态对象正在填充表单数据,但是当它到达 spring 控制器时,模态对象具有所有它的属性为空,因此,我收到以下错误:
{"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"}
我做错了什么?
我认为这是因为您使用的 scope.modality 小写。
Javascript 变量区分大小写。
我正在使用 AngularJS 来 post 一个用 Spring 引导和休息编写的 API 的简单形式。每个 class 都用 @RestController
注释我的spring控制器方法如下:
@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>
Post 通过 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);
});
}
当我使用 angular 提交表单时,我可以看到模态对象正在填充表单数据,但是当它到达 spring 控制器时,模态对象具有所有它的属性为空,因此,我收到以下错误:
{"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"}
我做错了什么?
我认为这是因为您使用的 scope.modality 小写。
Javascript 变量区分大小写。