参数不会从前端传递到后端

时间:2016-02-10 20:11:43

标签: java angularjs rest

我必须说,我在AngularJS中完全是新手。我想在我的应用程序中添加注册选项。我有一些例子,我想添加寄存器选项。 我做过这样的事情:

var services = angular.module('exampleApp.services', ['ngResource']);


services.factory('UserService', function ($resource) {

return $resource('rest/user/:action', {},
        {
            authenticate: {
                method: 'POST',
                params: {'action': 'authenticate'},
                headers: {'Content-Type': 'application/x-www-form-urlencoded'}
            }
        }
);
});


function LoginController($scope, $rootScope, $location, $cookieStore, UserService) {

$scope.rememberMe = false;

$scope.login = function () {

    UserService.authenticate($.param({username: $scope.username, password: $scope.password}), function (authenticationResult) {
        var authToken = authenticationResult.token;
        $rootScope.authToken = authToken;
        if ($scope.rememberMe) {
            $cookieStore.put('authToken', authToken);
        }
        UserService.get(function (user) {
            $rootScope.user = user;
            $location.path("/");
        });
    });
};

$scope.register = function () {
    $scope.user = new UserService();
    console.log($scope.user);
    $scope.user.$save(function () {
        $location.path('/');
    });

};

} ;

angular.module('exampleApp', ['ngRoute', 'ngCookies', 'exampleApp.services'])
    .config(
            ['$routeProvider', '$locationProvider', '$httpProvider', function ($routeProvider, $locationProvider, $httpProvider) {

                    $routeProvider.when('/create', {
                        templateUrl: 'partials/create.html',
                        controller: CreateController
                    });

                    $routeProvider.when('/login', {
                        templateUrl: 'partials/login.html',
                        controller: LoginController
                    });
        //more stuff here

});

@Component
@Path("/user")
public class UserResource {

    //some field here

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    public User register(User user) {
          return this.userDao.save(user);
    }

问题是,我正在使用java register方法处理,但我的User参数为null。

我也尝试过使用:

    @POST
@Produces(MediaType.APPLICATION_JSON)
public User register( @QueryParam("password") String password, @QueryParam("username") String username) {

    return this.userDao.save(new User(username, password));
}

 $scope.register = function () {
    $scope.user = new UserService();
    console.log($scope.user);

    $http({
        url: 'user',
        method: 'POST',
        params: {
            username: $scope.username,
            password: $scope.password
        }
    }).success(function (data) {
        $scope.persons = data;
    });

};

但我收到了

Failed to load resource: the server responded with a status of 404 (Not Found)

在浏览器中。

1 个答案:

答案 0 :(得分:0)

你正确地从Angular.JS传递JSON有问题。

您不应该尝试使用$.params自行序列化JSON对象,按原样传递对象并让Angular处理它,而不是:

UserService.authenticate({username: $scope.username, password: $scope.password}, function (authenticationResult) {

您应该正确地指出您要发送JSON,而不是'application/x-www-form-urlencoded'

return $resource('rest/user/:action', {},
    {
        authenticate: {
            method: 'POST',
            responseType: 'json',
            params: {'action': 'authenticate'},
            headers: {
                'Content-Type': 'application/json; charset=UTF-8'
            }
        }
    }

初始代码的其余部分看起来很好,在Java端不需要@QueryParam,您将发送并返回JSON,@QueryParam用于在URL本身内发送params的情况

但有一件事。你在Angular.JS方面有authenticate个动作,但在Java方面,你有register个动作。动作名称显然应该匹配。