Gabriel R.
Gabriel R.

Reputation: 328

Spring MVC - RequestParamException parameter is not present

I've got an issue that occurs eventually in my website. It uses AJAX requests to get data from the server, which uses Spring MVC.

What happens (intermittently) is that sometimes we got an exception like this one:

org.springframework.web.bind.MissingServletRequestParameterException: Required Integer parameter 'page' is not present
at 

This kind of exception occurs in some AJAX POST calls (not only for this case!!) and we still cannot reproduce it to understand what is happening.

For example, in one of the cases the parameter 'page' (used to load content while the user scrolls the page - so it's a required variable) is being sent through an AJAX call that has a 'data' field with the page parameter coming from a form like this:

<input type="hidden" name="page" id="page" value="1">

And a ajax call like this one (both $("#filter") and url are ok):

$.ajax({
    type: "POST",
    data: $("#filter").serialize(), // serializes the form's elements.
    url: _ctx + URL_FILTER,
    cache: false
})

The only way we got to reproduce that is by changing its property 'name' to something other than "page". But I guess this is not the case (most users don't even open the developer console...)

I've googled it a lot and I checked every possibility. The enconding is ok:

(Content-Type: application/x-www-form-urlencoded; charset=UTF-8) 

The parameters are ok, the AJAX looks ok, everything seems ok... But we cannot find what is going on. We have tried a lot of possibilities but we still couldn't force these exceptions to happen.

One hypothesis we have is that sometimes the AJAX may send empty data blocks, with none of the parameters. But we don't even know whether it's true or not and how to check its veracity.

What are the possibilities? How can it be tested?

EDIT: We could reproduce one of the ways to get the Exception: Reloading the page repeatedly for some seconds (keeping the reload key pressed for a while). Is there a way to prevent the exception for this case?!

Upvotes: 14

Views: 2548

Answers (3)

Paul Podgorsek
Paul Podgorsek

Reputation: 2643

Are you sure your form isn't being modified at the same time? For example, if your library used to handle the scrolling of the page tries to send the same form in a very short time (the first call updates the form while the second call is serializing it). This might be only on some browsers, not all of them, given that you can't easily reproduce it.

The way data is put back in the form might also be responsible for your problem.

Logging HTTP requests / using analytics would help you identify which requests / user agents are causing issues.

Upvotes: 1

Durgesh
Durgesh

Reputation: 205

If you are not been able to figure out what is reason behind missing parameter, so you can add

public void controllerMethodName (@RequestParam(required = false) int page)

code in your controller definition which will not throw any exception if parameter is not present in your ajax request.

Upvotes: 1

Gaurav Srivastav
Gaurav Srivastav

Reputation: 2551

Make the below change to controller's class method for page parameter @RequestParam(defaultValue = 0) int page.

Or paste the controller method here.

Upvotes: 1

Related Questions