开发者

How to make Spring MVC Controller error out if given a query param that is not valid or with a different case?

开发者 https://www.devze.com 2023-01-16 08:12 出处:网络
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 s

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消