1

下面的代码工作正常,但我不知道在发送两个数据对象的情况下如何处理请求。

        //angular 
        $scope.data = //item object
        $http({
            method : 'POST',
            url : '/items',
            data : $scope.data,
            headers : {
                'Content-Type' : 'application/json'
            }
        }).success(function(data) {
        //...
        });
       //java rest
       @RequestMapping(value="/items", method=RequestMethod.POST)
       public ResponseEntity<?> createIslem(@RequestBody Item item){ 
       //....
       }

我的 java 控制器方法签名应该如何处理下面的请求?

        //angular 
        $scope.data = //item object
        $http({
            method : 'POST',
            url : '/items',
            //data1 is of type Item and data2 is of type AnotherObject
            data : {data1: $scope.data1, data2: $scope.data2}
            headers : {
                'Content-Type' : 'application/json'
            }
        }).success(function(data) {
        //...
        });
4

1 回答 1

2

好吧,你应该有一个像下面这样的类:

public class Command {
    private Item data1;
    private AnotherObject data2;
    // getters and setters omitted for brevity
}

并且该方法应声明为

public ResponseEntity<?> createIslem(@RequestBody Command command)

以便 Java 对象结构与您发送的 JavaScript 对象的结构相匹配。

于 2015-08-29T12:37:26.173 回答