java - 无法在 Java 中对数组的元素执行 Bean 验证
问题描述
我有一个 Spring Boot 应用程序,我需要在其中执行对象验证。
必须验证的对象:
public class MyObject {
@NotNull
@Size(min=5)
@ApiModelProperty(value = "first name", required=true)
private String firstName;
//getters and setters for firstName
}
现在在我的控制器中,我有两种方法-
public ResponseEntity<RespObject> getSingleResp(@Valid @RequestBody MyObject myObj) {
//Bean validation works I get a 400 if validation fails, only if it is valid then execution enters this method
}
public ResponseEntity<RespObject[]> getMultiResp(@Valid @RequestBody MyObject[] myObjs) {
//Bean validation does not happen here and starts execution of this method
}
我也尝试@NotNull
@NotEmpty
在两者之间添加@Valid @RequestBody
并MyObject[] myObjs
查看是否发生了验证,但它似乎不起作用。
我是否在这里遗漏了一些愚蠢的东西,或者 Bean 验证不适用于数组的各个元素?如果它不起作用,那么是否有解决方法来解决这个问题?
我为 List 找到了一个类似的问题,但那里的解决方案似乎对我不起作用。在 Spring 中验证对象列表
解决方案
推荐阅读
- java - 在 LinearLayout 底部和我的应用程序底部之间居中图像
- java - 指数函数参数
- css - 位置:固定和变换:translateZ
- python - 如何制作具有分类值的数据透视表?
- material-ui - 如何为同一内容配置多个 Material UI 徽章
- ruby - 具有属性路径一元 * 运算符语法的 Ruby SPARQL 客户端
- php - 使 Doctrine 的 UniqueEntity 约束不区分大小写
- python - 没有 sudo 的 Cron 作业重启
- wordpress - 无法翻译我的 wordpress 网站 - 没有 .pot 文件
- image-processing - 自然语言生成模型中如何控制作者的观点?