[英]Spring annotation validation and indexing for nested objects
In Spring MVC, I have annotated my parent Dto and list of child Dto's with bean-validation annotations as below: 在Spring MVC中,我注释了我的父Dto和带有bean验证注释的子Dto列表,如下所示:
class ParentDto {
@NotBlank
private String parentName;
@Valid
private Set<ChildDto> childList;
//getter and setter
}
class ChildDto {
@NotBlank
private String childName;
//getter and setter
}
If childName is empty in one of the child objects then spring returns error message as below without the index of child object: 如果其中一个子对象中的childName为空,则spring返回如下所示的错误消息,而不包含子对象的索引:
[{"errorCode":"NotNull","field":"parentDto.childList[].childDto ","message":"may not be null"}]
How can I enable spring to return a message with index (telling which child has problem) something like below: 如何让spring返回带索引的消息(告诉哪个孩子有问题)如下所示:
[{"errorCode":"NotNull","field":"parentDto.childList[1].childDto ","message":"may not be null"}]
I figured out why index was missing from the error message. 我找出了错误消息中缺少索引的原因。 Since i was using Set instead of List for the collection of child objects therefore it was not able to index it. 由于我使用Set而不是List来收集子对象,因此它无法对其进行索引。 Once change to list it works fine. 一旦更改为列表,它可以正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.