gpt4 book ai didi

java - 根据其他字段 javax.validation 的条件从选项进行字段验证

转载 作者:行者123 更新时间:2023-11-30 10:04:51 31 4
gpt4 key购买 nike

如果存在另一个字段,我需要执行字段验证(它可以是值之一)。

import javax.validation.*;

class Person {

@NotBlank
private String name;

private Long groupId;

@Valid // if group id is not null, select one from available.
private String specialization;

// getters, setters.
}

class PersonValidaionLogic {

@Autowired
private SpecializationService specializationService;

public void validatePerson(final Person person) {
Long groupId = person.getGroupId();
if (groupId != null) {
Set<String> availableSpecializations = specializationService.getByGroupId(groupId);
if (!availableSpecializations.contains(specialization)) {
addValidationError("specialization is not valid");
}
}
}
}

有一个nice answer on how to validate multiple fields in a class with conditions on each other .

如何将 specializationServicegroupId 传递给 validator 。

最佳答案

欢迎分享您的解决方案或想法!这就是我解决这个问题的方法。

我在我的问题中使用了链接中的想法,但方式更简单。

首先,我解决了一个问题how to pass a Spring component or service into validator .我使用了一个包含对服务的静态引用的组件。

其次,我按照链接中的描述验证了整个对象。

这是代码!

1) 创建注解@PersonConstraint 并放入Person 类。这可能会有所帮助 https://www.baeldung.com/javax-validation-method-constraints

@Target({ TYPE })
@Retention(RUNTIME)
@Constraint(validatedBy = PersonValidator.class)
public @interface PersonConstraint {

String message() default "Specialization is not valid";

Class<?>[] groups() default { };

Class<? extends Payload>[] payload() default { };

CaseMode value();
}

2) 包含对服务的静态引用的组件。

@Component // Spring component.
class ServiceHolderComponent {

private static SpecializationService SPECIALIZATION_SERVICE;

@Autowired
public ServiceHolderComponent(final SpecializationService specializationService) {
GROUP_SERVICE = Validate.notNull(groupService); //apache lib
}

public static SpecializationService getSpecializationService() {
return SPECIALIZATION_SERVICE;
}
}

3) 和人 validator

public class PersonValidator implements ConstraintValidator<PersonConstraint, Person> {

private final SpecializationService specializationService;

public UserDynamicEnumValidator() {
this(ServiceHolderComponent.getSpecializationService());
}

public UserDynamicEnumValidator(final SpecializationService specializationService) {
this.specializationService = specializationService;
}

@Override
public boolean isValid(final Person person, final ConstraintValidatorContext context) {
final Long groupId = person.getGroupId();
if (groupId == null) {
return true; // We consider it valid.
}

final String specialization = person.getSpecializat();
if (StringUtils.isEmpty(specialization)) {
return true; // We consider it valid.
}

// I changed the code of the service, so it returns a set of strings - projection query and collectors to set.
final Set<String> availableSpecializationValuesByGroup = specializationService.findByValue(groupId);

if (!availableSpecializationValuesByGroup.contains(specialization)) {
// To display custom message
context.disableDefaultConstraintViolation();
context.buildConstraintViolationWithTemplate("Specialization is not valid").addConstraintViolation();
return false;
}

return true;
}
}

显示custom message in validator检查这个

关于java - 根据其他字段 javax.validation 的条件从选项进行字段验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55632288/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com