How to mark Spring MVC params as required

grep picture grep · Nov 27, 2014 · Viewed 14.1k times · Source

I have such method in spring controller

@RequestMapping(value = "/updateArticle", method = RequestMethod.POST)
@ResponseBody
public void updateArticle(Long id,  String name, String description) {
  ...
}

I want the id, and name to be REQUIRED. In the other words, if they are null values, then an exception must be thrown.

How can I do that? Is there any annotation for this or something like that?

Thanks

Answer

macias picture macias · Nov 27, 2014

Yes, there is. @RequestParam(required=true) See the docs.

Required flag is even true by default, so all you need to do is:

@RequestMapping(value = "/updateArticle", method = RequestMethod.POST)
@ResponseBody
public void updateArticle(@RequestParam Long id, @RequestParam String name, @RequestParam String description) {
  ...
}