Kumar Narendra Kumar Narendra - 3 months ago 14
Java Question

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

I am getting 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 question in stackoverflow, but the given solutions didn't help.

Answer

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)