在restangular中通过Spring POST响应的url位置标头获取一个实体

时间:2015-01-03 12:23:49

标签: javascript spring rest restangular

如何将新发布的实体提取到Spring应用程序,该应用程序仅返回" location"创建资源的标题作为回应?我设法使用

从标题中获取网址
RestangularProvider.setFullResponse(true);
在restangular配置中的

(顺便说一下,有更优雅的方法吗?当前的解决方案迫使我在整个地方使用.data。)

响应如下:

Cache-Control   no-cache, no-store, max-age=0, must-revalidate
Date    Sat, 03 Jan 2015 11:50:56 GMT
Expires 0
Location     https://localhost:8443/users/54a7d7a0ccf25ba3e97a51f9
Pragma  no-cache
Server  Apache-Coyote/1.1
Strict-Transport-Security   max-age=31536000 ; includeSubDomains
Transfer-Encoding   chunked
X-Application-Context   application
X-Content-Type-Options  nosniff
X-Frame-Options DENY
X-XSS-Protection    1; mode=block

我的restangular post controller动作如下所示:

$scope.create = function () {
        $scope.users.post({
            name: $scope.name,
            surname: $scope.surname,
            title: $scope.title,
            email: $scope.email,
            password: $scope.password,
            phone: $scope.phone,
            skype: $scope.skype,
            roles: $scope.roles,
            nonLocked: $scope.nonLocked,
            nonExpired: $scope.nonExpired,
            credintialsNonExpired: $scope.credintialsNonExpired
        }).then(
                function (data) {
                    var location = data.headers('location');
                    var modal = $('#newUser');
                    modal.modal('hide');
                    modal.on('hidden.bs.modal', function () {
                        $(this).find('form')[0].reset();
                    });
                    $('#roles').selectpicker('deselectAll');
                },
                function (error) {
                    if (error.status === 409) {
                        $('.alert').removeClass('hidden');
                    } else {
                        alert("Niestety wystąpił błąd, czy na pewno wypełniłeś wszystkie pola?");
                    }
                }
        );

服务器端Spring资源:

@RepositoryRestResource
public interface UserRepository extends MongoRepository<User, String> {

    public User findOneByEmail(@Param("email") String email);
}

我应该如何使用restangular请求此特定资源以及如何将其推入集合以刷新视图?

1 个答案:

答案 0 :(得分:0)

似乎我已经明白了。我需要使用的只是

users.oneUrl('users', location).get().then(
                                    function (newUser) {
                                        $scope.users.push(newUser);
                                    });

成功发布新用户后,所有内容都像魅力一样。