How to mark Spring MVC params as required

19k views Asked by At

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

4

There are 4 answers

5
makasprzak On BEST ANSWER

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) {
  ...
}
0
AudioBubble On
@RequestMapping (value = "/gethomeworkinfo.json", method = {RequestMethod.GET, RequestMethod.POST}, produces = "application/json")
public @ResponseBody
HomeworkReport getHomeworkReportInfo(@RequestParam (value = "schoolid", required = false) String schoolId, @RequestParam (value = "homeworkid") String homeworkId,
        @RequestParam (value = "startdate", required = false, defaultValue = "2014-06-01") @DateTimeFormat (pattern = "yyyy-MM-dd") Date startdate,
        @RequestParam (value = "enddate", required = false, defaultValue = "2014-12-30") @DateTimeFormat (pattern = "yyyy-MM-dd") Date enddate,
        @RequestParam (value = "userid", required = false) String userId, HttpServletRequest request) {

    // some actions

}

I currently use this method. It will help you.

5
blackpanther On

I assume you are passing a form to this controller. Therefore, convert all the form fields into a Form object and then you can use Java validations on them. So, for example, you have the id, name and description fields in the form:

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

public class ArticleForm {

    @NotNull
    private Long id;

    @Size(min = 2, max = 20)
    private String name;

    @Size(min =2, max =20)
    private String description;

    // getters and setters
}

Then, in your controller:

 @RequestMapping(value = "/updateArticle", method = RequestMethod.POST)
 @ResponseBody
 public void updateArticle(@Valid ArticleForm articleForm) {
      // do something with the article form object using getters and this retrieves
      // values sent in the HTTP Form
 }
0
Oomph Fortuity On

You have to add RequestParam with required true annotation in your controller

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

When you will hit this controller URL from browser and if one of the param is mission then you will get incorrect syntax error on browser