![](/img/trans.png)
[英]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.