Search code examples
springspring-mvcspring-validator

How to mark Spring MVC params as required


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


Solution

  • 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) {
      ...
    }