[英]How to validate request parameter if it is not a bean in spring MVC?
Below is a POST end point in my spring MVC REST service. 下面是我的spring MVC REST服务中的POST端点。 I want to use spring validation frame work to make sure that list I receive is not empty.
我想使用spring验证框架工作来确保我收到的列表不为空。 How do I do it?
我该怎么做? Do I have to provide wrapper bean to around
listOfLongs
? 我是否必须在
listOfLongs
周围提供包装bean?
@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]}
Providing a wrapper bean is a good practice. 提供包装bean是一种很好的做法。
class LongList {
@NotEmpty
private List<Long> listOfLongs;
// Setters and Getters ...
}
Then, the Request Body should be { listOfLongs : [123,456,789]}
然后,请求
{ listOfLongs : [123,456,789]}
应为{ listOfLongs : [123,456,789]}
@RequestMapping(value = "/some/path", method = RequestMethod.POST)
@ResponseBody
public Foo bar(@Valid @RequestBody LongList listOfLongs) {
// do some useful work
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.