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

32,981

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)
Share:
32,981
Kumar Narendra
Author by

Kumar Narendra

Updated on February 10, 2020

Comments

  • Kumar Narendra
    Kumar Narendra about 4 years

    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.