Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?!

like image 999
Gabriel R. Avatar asked Oct 05 '15 19:10

Gabriel R.


People also ask

Is RequestParam required by default?

Method parameters annotated with @RequestParam are required by default. will correctly invoke the method. When the parameter isn't specified, the method parameter is bound to null.

What is the difference between @RequestBody and @RequestParam?

@RequestParam makes Spring to map request parameters from the GET/POST request to your method argument. @RequestBody makes Spring to map entire request to a model class and from there you can retrieve or set values from its getter and setter methods.

What is MissingServletRequestParameterException?

MissingServletRequestParameterException(String parameterName, String parameterType, boolean missingAfterConversion) Constructor for use when a value was present but converted to null .

What does this mean required string parameter nonce is not present?

The spring boot error Required string parameter is not present occurs when the request parameter is not stated in the request url and the request parameter is configured as mandatory.


1 Answers

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

Or paste the controller method here.

like image 67
Gaurav Srivastav Avatar answered Sep 23 '22 19:09

Gaurav Srivastav