Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Below is a POST end point in my spring MVC REST service. I want to use spring validation frame work to make sure that list I receive is not empty. How do I do it? Do I have to provide wrapper bean to around listOfLongs?

    @RequestMapping(value = "/some/path", method = RequestMethod.POST)
    @ResponseBody
    public Foo bar(@Valid @NotEmpty @RequestBody List<Long> listOfLongs) {

     /*   if (listOfLongs.size() == 0) {
            throw new InvalidRequestException();
        }
     */

        // do some useful work
    }

What should be the Request Body?

1) [123,456,789]
2) { listOfLongs : [123,456,789]}
share|improve this question

1 Answer

Providing a wrapper bean is a good practice.

class LongList {

 @NotEmpty
 private List<Long> listOfLongs;

 // Setters and Getters ...

}

Then, the Request Body should be { listOfLongs : [123,456,789]}

@RequestMapping(value = "/some/path", method = RequestMethod.POST)
@ResponseBody
public Foo bar(@Valid @RequestBody LongList listOfLongs) {   

    // do some useful work
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.