如何将值从AngularJS传递给Spring REST控制器

时间:2015-11-19 05:20:28

标签: angularjs spring

我是AngularJS和Spring MVC的新手。我试图从AngularJS调用REST服务并验证登录凭据。

我不确定我是否正在尝试以正确的方式传递参数以将其传递给Controller。如果我错过任何

,有人可以指导我

Controller.js

var SupportSystems = angular.module('SupportSystems', [ 'ngResource' ]);
SupportSystems
        .controller(
                'LoginController',
                function($scope, $resource) {
                    $scope.authenticate = function() {
                        var authenticateUser = $resource('http://localhost:8080/SupportSystems/SSystems/authenticateUser/:userName', {userName: '@userName'});
                        authenticateUser.save(function(data) {
                            alert("user->" + $scope.userName);
                            alert("pass->" + $scope.password);
                        });
                    }
                });

Spring控制器

@RequestMapping("/SSystems")
public class SupportSystemsController { `

    @RequestMapping(value="/authenticateUser/{userName}", method=RequestMethod.POST)
    public ResponseEntity<Boolean> authenticateUser(@RequestParam("userName") String userName) {
        System.out.println("username -->" + userName);
        return new ResponseEntity<Boolean>(Boolean.TRUE, HttpStatus.OK);
    }

错误:必需字符串参数'userName'不存在。

我需要将用户名和密码的值传递给Spring控制器。我正在尝试使用@RequestParam

<body ng-controller="LoginController">

<form class="login-form" name="loginForm" ng-submit="authenticate()">       
    <h2> Login</h2>
    <input type="text" id="userName" class="form-control" name="userName" required autofocus="autofocus" 
    ng-model="userName" />

1 个答案:

答案 0 :(得分:1)

回答原始问题: 您需要使用@PathVariable

@RequestParam instread

建议:我建议您将usernamepassword数据作为POST请求的正文提交。

如何使用POST发送$http请求:

$scope.authenticate = function() {
    var data = {
        username: $scope.username,
        password: $scope.password
    };

    var successCallBack = function(response){
        // success response found from server
    };

    var errorCallBack = function(response){
        // error response found from server
    };

    $http.post('http://localhost:8080/SupportSystems/SSystems/authenticateUser', data).then(successCallBack, errorCallBack);
}

如何在Spring控制器中处理它:

@RequestMapping(value="/authenticateUser", method=RequestMethod.POST)
public ResponseEntity<?> authenticateUser(@Valid @RequestBody AuthenticateUserRequest request) {

    // check the submitted username and password
    if(request.getUserName().equals("nayan") && request.getPassword().equals("pass")){
        return new ResponseEntity<>(null, HttpStatus.OK);
    } else {
        return new ResponseEntity<>(null, HttpStatus.BAD_REQUEST);
    }
}

您的AuthenticateUserRequest课程可能如下所示:

public class AuthenticateUserRequest {

    @NotNull
    private String username;

    @NotNull
    private String password;

    public String getUsername(){return this.username;}
    public void setUsername(String username){this.username = username;}
    public String getPassword(){return this.password;}
    public void setPassword(String password){this.password = password;}

}

备注

  1. @Valid注释是可选的。如果存在,它将确保必须将usernamepassword提交给控制器。 @NotNull注释会检查此验证。
  2. 在大多数情况下,您会将提交的usernamepassword值与数据库中的值进行匹配。为了简单起见,我已经跳过了。