[英]How to force a bean request parameter to be required in Spring Rest MVC?
[英]How to validate request parameter if it is not a bean in spring MVC?
下面是我的spring MVC REST服务中的POST端点。 我想使用spring验证框架工作来确保我收到的列表不为空。 我该怎么做? 我是否必须在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
}
请求机构应该是什么?
1) [123,456,789]
2) { listOfLongs : [123,456,789]}
提供包装bean是一种很好的做法。
class LongList {
@NotEmpty
private List<Long> listOfLongs;
// Setters and Getters ...
}
然后,请求{ 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.