java 是否有注释验证 Objects 列表中字段的值是否重复?
Is there an annotation for java validate if the value of a field in a List of Objects is duplicated?
我有一个 Object 的列表,每个 Object 都有一封电子邮件,我想验证电子邮件是否在此列表中重复。
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
Father create(@PathVariable String id,
@Valid @RequestBody Father father) {
...
}
父亲会有一个列表 child:
private List<Child> childs;
每个 child 都会有一个电子邮件地址:
public class Child {
...
@NotEmpty
private String email;
...
}
我想验证是否有一个请求正文包含 2 child 个相同的电子邮件。
是否可以或仅在接收并处理有效载荷后进行验证?
您可以使用 @UniqueElements
Hibernate 注释,您可以在 this documentation 上找到它,这是基于某些元素的 equals
。
@UniqueElements
private List<Child> childs;
已编辑
要验证子电子邮件列表,您可以创建自定义验证。
我编写了一个自定义验证代码如下
1- Create annotation named ChildEmailValidation
@Documented
@Constraint(validatedBy = ChildEmailValidator.class)
@Target( { ElementType.METHOD, ElementType.FIELD })
@Retention(RetentionPolicy.RUNTIME)
public @interface ChildEmailValidation {
String message() default "Duplicate Email";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
2- Create a validator for ChildEmailValidation
在这部分,您可以编写自定义业务进行验证。 (你可以写你的算法)
public class ChildEmailValidator implements ConstraintValidator<ChildEmailValidation, List<Child>> {
@Override
public void initialize(ChildEmailValidation constraintAnnotation) {
}
@Override
public boolean isValid(List<Child> childList, ConstraintValidatorContext constraintValidatorContext) {
//Create empty mailList
List<String> mailList = new ArrayList<>();
//Iterate on childList
childList.forEach(child -> {
//Checks if the mailList has the child's email
if (mailList.contains(child.getMail())) {
//Found Duplicate email
throw new DuplicateEmailException();
}
//Add the child's email to mailList (If duplicate email is not found)
mailList.add(child.getMail());
});
//There is no duplicate email
return true;
}
}
3- Add @ChildEmailValidation
in Father class
public class Father {
List<Child> child;
@ChildEmailValidation
public List<Child> getChild() {
return child;
}
public void setChild(List<Child> child) {
this.child = child;
}
}
4- Put @Valid
on fatherDto
in the controller
@RestController
@RequestMapping(value = "/test/", method = RequestMethod.POST)
public class TestController {
@RequestMapping(value = "/test")
public GenericResponse getFamily(@RequestBody @Valid Father fatherDto) {
// ...
}
}
我有一个 Object 的列表,每个 Object 都有一封电子邮件,我想验证电子邮件是否在此列表中重复。
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
Father create(@PathVariable String id,
@Valid @RequestBody Father father) {
...
}
父亲会有一个列表 child:
private List<Child> childs;
每个 child 都会有一个电子邮件地址:
public class Child {
...
@NotEmpty
private String email;
...
}
我想验证是否有一个请求正文包含 2 child 个相同的电子邮件。
是否可以或仅在接收并处理有效载荷后进行验证?
您可以使用 @UniqueElements
Hibernate 注释,您可以在 this documentation 上找到它,这是基于某些元素的 equals
。
@UniqueElements
private List<Child> childs;
已编辑
要验证子电子邮件列表,您可以创建自定义验证。
我编写了一个自定义验证代码如下
1- Create annotation named ChildEmailValidation
@Documented
@Constraint(validatedBy = ChildEmailValidator.class)
@Target( { ElementType.METHOD, ElementType.FIELD })
@Retention(RetentionPolicy.RUNTIME)
public @interface ChildEmailValidation {
String message() default "Duplicate Email";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
2- Create a validator for
ChildEmailValidation
在这部分,您可以编写自定义业务进行验证。 (你可以写你的算法)
public class ChildEmailValidator implements ConstraintValidator<ChildEmailValidation, List<Child>> {
@Override
public void initialize(ChildEmailValidation constraintAnnotation) {
}
@Override
public boolean isValid(List<Child> childList, ConstraintValidatorContext constraintValidatorContext) {
//Create empty mailList
List<String> mailList = new ArrayList<>();
//Iterate on childList
childList.forEach(child -> {
//Checks if the mailList has the child's email
if (mailList.contains(child.getMail())) {
//Found Duplicate email
throw new DuplicateEmailException();
}
//Add the child's email to mailList (If duplicate email is not found)
mailList.add(child.getMail());
});
//There is no duplicate email
return true;
}
}
3- Add
@ChildEmailValidation
in Father class
public class Father {
List<Child> child;
@ChildEmailValidation
public List<Child> getChild() {
return child;
}
public void setChild(List<Child> child) {
this.child = child;
}
}
4- Put
@Valid
onfatherDto
in the controller
@RestController
@RequestMapping(value = "/test/", method = RequestMethod.POST)
public class TestController {
@RequestMapping(value = "/test")
public GenericResponse getFamily(@RequestBody @Valid Father fatherDto) {
// ...
}
}