zzangs33
zzangs33

Reputation: 307

(Spring, AngularJS)I don't know why it is NULL

I wanna send user's email to check duplicate

This is my Back Controller

@RequestMapping(value = "/api/v1/public/checkDuplicate", method = RequestMethod.POST)
public ResponseEntity<Object> getCnt(
        HttpServletRequest request,
        HttpServletResponse response,
        @RequestParam(value="usrEmail", required=false) String usrEmail){
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
}

And this is my Front Controller

$scope.user.usrEmail = $scope.usrEmail;
var req = {
    method: 'POST',
    url: './api/v1/public/checkDuplicate',
    dataType: 'json',
    headers: { 
        'Content-Type': 'application/json; charset=utf-8'
    },
    data: angular.toJson($scope.usrEmail)
};

And this is my View

<input type="email" class="form-control" name="usr_Email"
       ng-model="usrEmail" ng-required="true" 
       ng-keypress="checkDuplicate()">

When I checked using 'console.log', Front Controller can get user's email properly. But in Back Controller, RequestParam has nothing, just NULL without Error Code except 'NullPointerException'.

It isn't communication error I think. Are there something I missed? Thanks!

Upvotes: 0

Views: 202

Answers (4)

Prasad
Prasad

Reputation: 1157

you didn't add query parameter in the angular

     url: './api/v1/public/checkDuplicate?**usrEmail='+$scope.usrEmail**

add this to you code ?usrEmail='+$scope.usrEmail

Upvotes: 1

Akshaya Jeevan
Akshaya Jeevan

Reputation: 634

Its the @RequestParam that you are using in Back Controller as it is linked to request params. It should be @RequestBody which is linked to the HTTP request body. Check here for more info.

Now you can change your code like below -

Back Controller

    @RequestMapping(value = "/api/v1/public/checkDuplicate", method = RequestMethod.POST)
    public ResponseEntity<Object> getCnt(
            HttpServletRequest request,
            HttpServletResponse response,
            @RequestBody Object userReqObj){
            HashMap<String, Object> userMap = (HashMap<String, Object>) userReqObj;
            String userEmail = userMap.get("usrEmail");
            //logic goes here...
    }

Front Controller

var req = {
        method: 'POST',
        url: './api/v1/public/checkDuplicate',
        dataType: 'json',
        headers: { 
            'Content-Type': 'application/json'
        },
        data: {
             'usrEmail' : $scope.usrEmail
       }
    };

Upvotes: 1

Sangram Badi
Sangram Badi

Reputation: 4264

Send your data from angularjs like below

$scope.user.usrEmail = $scope.usrEmail;
    var req = {
        method: 'POST',
        url: './api/v1/public/checkDuplicate',
        dataType: 'json',
        headers: { 
            'Content-Type': 'application/json; charset=utf-8'
        },
        data: {
             'usrEmail' : $scope.usrEmail
       }
    };

Use RequestBody annotaion to get email as you are passing data from body. and need to create a Bean class for user.

@RequestMapping(value = "/api/v1/public/checkDuplicate", method = RequestMethod.POST)
public ResponseEntity<Object> getCnt(
        HttpServletRequest request,
        HttpServletResponse response,
        @RequestBody User usrEmail){
        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
}

OR

If you want to use PathVariable then you need to pass email from your URL in angularjs.

 url: './api/v1/public/checkDuplicate/'+$scope.usrEmail

Then you need to use @PathValiable('usrEmail') in your controller and @RequestMapping(value = "/api/v1/public/checkDuplicate/{usrEmail}"

Upvotes: 1

31piy
31piy

Reputation: 23859

Please read the docs for $http config object. For the key data, it says:

data – {string|Object} – Data to be sent as the request message data.

This means that data can be an object or a string. In your case, you're sending it as a string, which makes sense, but you forgot to mention the name of the parameter in it. The data in your code should be the following:

data: 'usrEmail=' + $scope.usrEmail // there is no need of angular.toJson here

This correctly signifies the name of the parameter as usrEmail. But to avoid the confusion, you can always use an object to specify the data:

data: {
  usrEmail: $scope.usrEmail
}

Upvotes: 1

Related Questions