Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 340
grep Avatar asked Nov 27 '14 09:11

grep


1 Answers

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) {
  ...
}
like image 81
macias Avatar answered Sep 27 '22 22:09

macias