0

AngularJS を使用して、Spring ブートと 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> &nbsp;
                <input type="reset" value="Reset" /> &nbsp;
            </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);
        });
}

angularを使用してフォームを送信すると、モダリティオブジェクトにフォームデータが入力されていることがわかりますが、スプリングコントローラーに到着すると、モダリティオブジェクトのすべての属性がnullになり、そのため、次のエラーが発生します:

{"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"}

私は何を間違っていますか?

4

1 に答える 1

0

小文字でscope.modalityを使用しているためだと思います。

Javascript 変数は大文字と小文字が区別されます。

http://www.w3schools.com/js/js_variables.asp

于 2015-11-25T16:03:41.213 に答える