I have a web service written in Spring MVC. It can be used by third-party developers. Our methods have many optional parameters (passed in the query string).
I want to make sure that all query string parameters are spelled correctly and that there are no typos. Is there an easy way to do this? Method signature example:
@RequestMapping(value = {"/filter"}, method = RequestMethod.GET) @ResponseBody public List<MetricType> getMetricTypes( @RequestParam(value = "subject", required = false) Long subjectId, @RequestParam(value = "area", required = false) Long areaId, @RequestParam(value = "onlyImmediateChildren", required = false) Boolean onlyImmediateChildren, @RequestParam(value = "componentGroup", required = false) Long componentGroupId ) throws Exception {
If someone calls this method the parameter "onlyImediateChildren = true" (typo) instead of "onlyImmediateChildren = true", Spring MVC will ignore the sealed parameter and suppose that "onlyImmediateChildren" is null. The developer will receive a slightly incorrect list of results and will not notice an error. Such problems can be widespread and difficult to diagnose. I want to check for sealed parameters in the query string to prevent such problems.
UPDATE
A list of actual parameters can be retrieved from the query string. Then it can be compared with a list of allowed parameters. If I copy the list of allowed parameters, it will duplicate the method signature. I wonder if it is easy to extract a list of allowed parameters from a method signature (for example, by annotating @RequestParam)?
Many thanks
Maksim
Maxim eliseev
source share