One of the requirements of a web service I'm working on is that if a query param name is given that is either invalid or not case-exact with what's expe开发者_运维百科cted, it should give an error saying so.
Is there anything in Spring MVC that can do this via configuration or some kind of setting? I imagine it's possible to code it, but I think that'd be a lot uglier.
The way I do this is to write a base class that defines methods for getting mandatory and optional query parameters. These throw exceptions that are intercepted (again) in the base class and turned into 400
responses.
I would not bother with trying to diagnose query parameters whose name is unrecognised / has the wrong case. Query parameter names are typically hard-wired into HTML, Javascript, etc written by developers. They should be able to find out what is valid by reading the web-api documentation. Once they've debugged their code, any special code in the servlet to pick up unexpected parameters is just a runtime overhead.
精彩评论