0

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!

zzangs33
  • 197
  • 1
  • 15
  • Hey @zzangs33, i answered again. please check – Sangram Badi Apr 02 '18 at 08:49
  • @SangramBadi -- please don't poke OP to check your answer. If it solved the problem, they will apparently upvote/accept it. – 31piy Apr 02 '18 at 09:11
  • When the client is POSTing the data as an object or a string, the server receives it in RequestBody and not RequestParam. Use RequestBody and see what object you receive. – Rangamannar Apr 02 '18 at 09:30

4 Answers4

1

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
}
31piy
  • 23,323
  • 6
  • 47
  • 67
  • I tried changing code as you write, It didn't work too. and I also tried sending RequestBody as map`$scope.user.usrEmail = $scope.usrEmail; ~~ data : angular.toJson($scope.user)` – zzangs33 Apr 02 '18 at 08:59
  • @zzangs33 -- What's the error? Did you try to make data as string? – 31piy Apr 02 '18 at 09:12
  • `public ResponseEntity getCnt( HttpServletRequest request, HttpServletResponse response, @RequestParam(value="usrEmail", required=false) String usrEmail)` – zzangs33 Apr 02 '18 at 09:13
  • `var req = { method: 'POST', url: './api/v1/public/checkDuplicate', dataType: 'json', headers: { 'Content-Type': 'application/json; charset=utf-8' }, 'usrEmail' : $scope.usrEmail };` – zzangs33 Apr 02 '18 at 09:13
  • I only changed code in the JS. but it is still NULL – zzangs33 Apr 02 '18 at 09:15
  • @zzangs33 -- On JS side, your code was correct, except in the `data`, instead of `angular.toJson($scope.usrEmail)` you need to write `'usrEmail=' + $scope.usrEmail`. Let me know if that works. – 31piy Apr 02 '18 at 09:39
  • I send Object's JSON as RequestBody Map map, and then it works! I don't know why it isn't work when I send only String as RequestParam.... – zzangs33 Apr 03 '18 at 00:28
1

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}"

Sangram Badi
  • 4,054
  • 9
  • 45
  • 78
  • `'usrEmail' : $scope.usrEmail` , `@RequestParam(value="usrEmail", required=false) String usrEmail)` – zzangs33 Apr 02 '18 at 09:16
  • then you need to create a bean for user, which may conatins getter setter method of email. Or you can pass email is from URL and can use PathVariable annotaion – Sangram Badi Apr 02 '18 at 09:18
  • `@RequestMapping(value = "/api/v1/doc/list", method = RequestMethod.GET) public ResponseEntity getList( HttpServletRequest request, HttpServletResponse response, @RequestParam(value="formId", required=false) String formId, @RequestParam(value="currentPage", defaultValue="1", required=false) int currentPage, @RequestParam(value="docTitle", required=false) String docTitle, @RequestParam(value="companyId", required=false) String companyId, @RequestParam(value="order", required=false) String order)` This code goes well.. but I don't know what is diffrent. – zzangs33 Apr 02 '18 at 09:26
  • I send Object's JSON as RequestBody Map map, and then it works! I don't know why it isn't work when I send only String as RequestParam.... – zzangs33 Apr 03 '18 at 00:28
1

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
       }
    };
Akshaya Jeevan
  • 624
  • 5
  • 13
  • I send Object's JSON as RequestBody Map map, and then it works! I don't know why it isn't work when I send only String as RequestParam.... – zzangs33 Apr 03 '18 at 00:28
  • @zzangs33 For `@RequestParam` you have to send the string as request params like `./api/v1/public/checkDuplicate?usrEmail=some@email.com` – Akshaya Jeevan Apr 03 '18 at 05:00
1

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

Prasad
  • 1,089
  • 13
  • 21