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

2

7

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.

Donata answered 26/8, 2015 at 8:4 Comment(1)
Can you show the JSON which is being posted. I think the JSON you are posting is not in proper format. you are sending only the value. You need to send it in key/value pair e.g. {"userId":123}Plovdiv
Z
10

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)
Zetta answered 26/8, 2015 at 8:14 Comment(1)
@RequestBody annotation doesn't have a value propertyConsistory
B
0
@GetMapping
    public List<PostCreateRequest> getAllPost(@RequestParam(required = false, defaultValue = "0") Integer id){
          return postservice.getAllpost(id);
    } 

this is my works. @RequestParam(required = false, defaultValue = "0") here is really worked!

Brutish answered 17/4, 2023 at 19:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.