本文介绍了如果不是Spring MVC中的bean,如何验证请求参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
以下是我的Spring MVC REST服务中的POST端点.我想使用Spring验证框架来确保收到的列表不为空.我该怎么做?我是否必须在listOfLongs
附近提供包装bean?
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
}
请求主体是什么?
1) [123,456,789]
2) { listOfLongs : [123,456,789]}
推荐答案
提供包装bean是一个好习惯.
Providing a wrapper bean is a good practice.
class LongList {
@NotEmpty
private List<Long> listOfLongs;
// Setters and Getters ...
}
然后,请求正文应为{ listOfLongs : [123,456,789]}
@RequestMapping(value = "/some/path", method = RequestMethod.POST)
@ResponseBody
public Foo bar(@Valid @RequestBody LongList listOfLongs) {
// do some useful work
}
这篇关于如果不是Spring MVC中的bean,如何验证请求参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!