最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Spring MVC - RequestParamException parameter is not present - Stack Overflow

programmeradmin2浏览0评论

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

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

Share Improve this question edited Oct 30, 2015 at 12:22 Gabriel R. asked Oct 5, 2015 at 19:55 Gabriel R.Gabriel R. 3283 silver badges16 bronze badges 10
  • 2 Can you log the HTTP requests received on the server ? It might help diagnose the issue. – Gaël J Commented Oct 5, 2015 at 20:03
  • Can u show how you defined your method in the controller, seems to me you are putting the parameter page as mandatory – jstuartmilne Commented Oct 5, 2015 at 20:08
  • @Sudakatux the parameter has to be mandatory, that's the problem! It should be there everytime the AJAX call is executed. Gaël I am not sure about that. How should I log the HTTP requests received on the server? And the problem is I don't know if I can generate a log file for every HTTP request (ing from this particular ajax call, for example) because it would have like 100.000 entries per hour... And probably just 0.001% would give me an exception – Gabriel R. Commented Oct 5, 2015 at 23:02
  • For cases like this i remend catching frontend errors and sending them to analytics or something like that. You can have various interceptors. – Laurentiu L. Commented Oct 8, 2015 at 10:45
  • 1 Paste your controller code. – Arpit Aggarwal Commented Oct 12, 2015 at 10:39
 |  Show 5 more ments

3 Answers 3

Reset to default 1 +25

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

Or paste the controller method here.

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.

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.

发布评论

评论列表(0)

  1. 暂无评论