方法一:
- 在controller层加上@Validated注解
- 在方法参数使用@valid注解即可
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/7c8461bd8f62a185214afb9356ab4f7f.png)
参考
2. 包装一层List
- 参数为json字符串,使用参数bean来接收,用列表来接收多个参数,在包装的列表类里使用@valid,原理都一样,在外层使用@Validated,内层使用@Valid。
@RequestMapping(value = "/", method = RequestMethod.POST)
public ResultVo applyUserCreate(@RequestBody @Validated JsonListParamWrapper<AccountConfigParam> param, HttpServletRequest request) {}
@Getter
@Setter
@Slf4j
public class JsonListParamWrapper<T> {
@Valid
@Size(min = 1,max = 100,message = "超过数量限制,单次提交至少1条,最多不超过100条")
List<T> content;
public JsonListParamWrapper(List<T> content) {
this.content = content;
}
public JsonListParamWrapper() {
this.content = new ArrayList<T>();
}
public List<CrawlSubmitInfoEntity> convertToEntity(Class<T> tClass, String userName) {
if (CrawlPointConfigParam.class.equals(tClass)) {
return this.content.stream().map(a -> {
CrawlPointConfigParam b = (CrawlPointConfigParam) a;
return b.convertToEntity(userName);
}).collect(Collectors.toList());
} else if (AccountConfigParam.class.equals(tClass)) {
return this.content.stream().map(a -> {
AccountConfigParam b = (AccountConfigParam) a;
return b.convertToEntity(userName);
}).collect(Collectors.toList());
} else {
log.error("参数处理异常");
return Lists.newArrayList();
}
}
@Override
public String toString() {
return "JsonListParamWrapper{" +
"content=" + content +
'}';
}
}
3. 自定义一层List