14

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

Gabriel R.
  • 303
  • 2
  • 15
  • 2
    Can you log the HTTP requests received on the server ? It might help diagnose the issue. – Gaël J Oct 05 '15 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 Oct 05 '15 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 (coming 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. Oct 05 '15 at 23:02
  • For cases like this i recommend catching frontend errors and sending them to analytics or something like that. You can have various interceptors. – Laurentiu L. Oct 08 '15 at 10:45
  • @Laurentiu do you have any suggestions on how to catch front-end errors? – Gabriel R. Oct 08 '15 at 11:25
  • @GabrielR. I had an angular approach in mind with http interceptors, but i think you can do something similar in jQuery (http://stackoverflow.com/questions/25076904/equivalent-of-angularjs-interceptor-in-jquery) . That is ofc for AJAX. This is a basic example of JS error tracking with google analytics http://davidwalsh.name/track-errors-google-analytics For all errors you can use something like http://stackoverflow.com/questions/5328154/catch-all-javascript-errors-and-send-them-to-server . – Laurentiu L. Oct 08 '15 at 11:45
  • can you copy the request as CURL ( using chrome dev tools - Network ) and the controller's method signature ( with annotations ) ? – Mourad Zouabi Oct 10 '15 at 16:45
  • 1
    Paste your `controller` code. – Arpit Aggarwal Oct 12 '15 at 10:39
  • @MouradZouabi I will paste for you. Arpit, I can show you the method, but it's not relevant since the exception is thrown when it gets called. – Gabriel R. Oct 12 '15 at 19:38
  • @GabrielR. I stumbled upon a similar issue where _intermittently_ spring throws the above exception even though the request paramter is set. Did you happen to come across any solutions/findings? – ameenhere May 21 '19 at 20:00

3 Answers3

1

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

Or paste the controller method here.

Gaurav Srivastav
  • 2,381
  • 1
  • 15
  • 18
  • Yes I know, but actually this parameter it has to be required to work as expected... And the method is "fine", it always work when we try to test it, but the problem is that sometime some users experience an issue that throws the exception described. – Davi Stuart Oct 14 '15 at 22:20
1

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.

Durgesh
  • 205
  • 2
  • 9
  • Yes I know, but actually this parameter it has to be required to work as expected... And the method is "fine", it always work when we try to test it, but the problem is that sometime some users experience an issue that throws the exception described. – Gabriel R. Oct 12 '15 at 19:37
1

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.

Paul Podgorsek
  • 2,416
  • 3
  • 19
  • 22