Restangular - 带有参数的getList发送到服务器

时间:2015-06-04 19:46:41

标签: javascript angularjs spring restangular spring-restcontroller

我在服务器端有方法:

@RequestMapping(method = RequestMethod.GET)
public List<UserDTO> getAllUsers(@RequestParam(value = "groupId", required = false) Long groupId,
                                 @RequestParam(value = "pagination") Pagination pagination){
    // some stuff
}

我尝试使用Restangular从客户端发送数据:

function getUsers(filters){
    restService.all("users").getList(filters).then(function(users){
        $scope.data.users = users;
    });
}
// ....
getUsers({groupId: undefined, pagination: $scope.data.pagination});

pagination可以是:

$scope.data = {
    // Other parameters
    pagination: {
        currentPage: 1,
        totalItems: 30,
        itemsPerPage: 5
    }
};

但我得到了:

Failed to convert value of type 'java.lang.String' 
to required type 'pagination.Pagination';

当我找到Request URL时,我看到了:

http://localhost:8080/skp/users?pagination=%7B%22currentPage%22:1,%22totalItems%22:30,%22itemsPerPage%22:5%7D

Query String Parameters是:

pagination:{"currentPage":1,"totalItems":30,"itemsPerPage":5}

我希望将分页对象发送到服务器端,但它会尝试发送字符串而不是对象。是否有任何想法,使用HTTP GET方法和getList Restangular方法发送参数不是Query String,我认为这会导致我的问题?或者如何让服务器端将其视为Pagination对象,而不是字符串?非常感谢你的答案

1 个答案:

答案 0 :(得分:1)

pagination=%7B%22currentPage%22:1,%22totalItems%22:30,%22itemsPerPage%22:5%7D

如我的评论中所述,这是一个值为字符串的参数。如果要在服务器端填充对象或输入bean,则必须在分页内发送三个值作为三个单独的参数。保持参数和bean字段名称相同以自动模拟这些字段