Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

org.springframework.web.bind.MissingServletRequestParameterException : Required Long parameter 'userId' is not present"

I am getting an exception while posting a call to Spring controller

Exception is:

 org.springframework.web.bind.MissingServletRequestParameterException : Required Long parameter 'userId' is not present"

My Javascript file is:

$scope.removeUser = function(user){
        var userId = JSON.stringify(user);
        $http.post('/portal/settings/user/deletecustomeruser', userId).success(function(data){
                $scope.response = data;
            })
        }
    }

The Spring controller code is:

      @RequestMapping( value = "/user/deletecustomeruser", method = RequestMethod.POST , headers="Accept=application/json")
public @ResponseBody String deleteCustomerUser( @RequestParam (value = "userId") Long userId,
        HttpServletRequest request )
                throws Exception
                {
    String returnString = "";
    User user = userService.getUserById( userId );

When I put (required = false), then the userId value became null. The JavaScript controller is clearly sending the "userId" in JSON format, but from the controller side there is some issue.

I checked almost all questions in stackoverflow, but the given solutions didn't help.

like image 795
Kumar Narendra Avatar asked Aug 26 '15 08:08

Kumar Narendra


1 Answers

You are expecting userId as request parameter on server side, but you are sending userId as request body in your client http post request,

Change the client side to send userId as request parameter

 $http.post('/portal/settings/user/deletecustomeruser?userId='+userId)

Or Change server side to use userId as request body in the controller

myControllerMethod (@RequestBody String userId)
like image 127
Anudeep Gade Avatar answered Nov 06 '22 20:11

Anudeep Gade