gpt4 book ai didi

java - 如何在 spring 4.* 中通过注解创建多个字段的约束 validator

转载 作者:行者123 更新时间:2023-12-02 14:04:22 25 4
gpt4 key购买 nike

如何在 spring 4.* 中通过注解创建更多字段的 validator 限制

@UniqueValidator

@Entity
@Table(name = "persons")
@UniqueValidator(message="Peson already exist",columns={"name","lastName"})
public class {
@Column
private String name;
@Column
private String lastName;
}

粗略地说..

select ... from persons where name='qwerty' and lastName='asdfgh'

最佳答案

这是在 Spring MVC 和 JSR 303 验证中执行此操作的一种方法:

创建约束注释:

package com.awgtek.model.validation;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import javax.validation.Constraint;
import javax.validation.Payload;

/**
* The target field should be unique in the data store.
*/
@Documented
@Constraint(validatedBy = UniqueNameValidator.class)
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface UniqueName {

/**
* @return the error message template
*/
String message() default "{com.awgtek.model.validation.UniqueName.message}";

/**
* @return the groups the constraint belongs to
*/
Class<?>[] groups() default {};

/**
* @return the payload associated to the constraint
*/
Class<? extends Payload>[] payload() default {};

/**
* @return a class an instance of which will be used to check the existence of a name.
*/
Class<? extends NameExistenceChecker> nameExistenceChecker();
}

在类级别将此注释应用于模型类:

package com.awgtek.model;

import com.awgtek.model.validation.MyNameExistenceChecker;
import com.awgtek.model.validation.UniqueName;
import com.awgtek.model.validation.UniqueNameConstraint;

@UniqueName(groups = UniqueNameConstraint.class,
nameExistenceChecker = MyNameExistenceChecker.class)
public class ConfigurationAttribute {

private String name;

private String lastName;

// getters and setters omitted

}

UniqueNameValidator:

package com.awgtek.model.validation;

import com.awgtek.service.MyService;

import javax.inject.Inject;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class UniqueNameValidator implements ConstraintValidator<UniqueName, Object> {

@Inject
private NameExistenceCheckerFactory nameExistenceCheckerFactory;

private NameExistenceChecker nameExistenceChecker;

@Override
public void initialize(UniqueName constraintAnnotation) {
nameExistenceChecker = nameExistenceCheckerFactory.get(constraintAnnotation.nameExistenceChecker());
}

@Override
public boolean isValid(Object value, ConstraintValidatorContext context) {
return !nameExistenceChecker.nameExists(value);
}
}

UniqueNameValidator 依赖于几个类:

package com.awgtek.model.validation;

import javax.inject.Inject;

import org.springframework.stereotype.Component;

@Component
public class NameExistenceCheckerFactory {

@Inject
private NamespaceNameExistenceChecker namespaceNameExistenceChecker;

@Inject
private MyNameExistenceChecker myNameExistenceChecker;

public NameExistenceChecker get(Class<? extends NameExistenceChecker> clazz) {
if (clazz.equals(MyNameExistenceChecker.class)) {
return myNameExistenceChecker;
}
throw new IllegalStateException("Unknown NameExistenceChecker");
}
}

package com.awgtek.model.validation;

public interface NameExistenceChecker {

boolean nameExists(Object objectWithName);
}

实际查找数据库中是否存在该项目的实现类(通过服务):

package com.awgtek.model.validation;

import com.awgtek.model.MyModelClass;
import com.awgtek.service.MyService;

import javax.inject.Inject;

import org.springframework.stereotype.Component;

@Component
public class AttributeNameExistenceChecker implements NameExistenceChecker {

@Inject
private MyService myService;

@Override
public boolean nameExists(Object objectWithName) {
MyModelClass myObject = (MyModelClass) objectWithName;
return myService.itemAlreadyExists(myObject.getName(), myObject.getLastName());
}
}

关于java - 如何在 spring 4.* 中通过注解创建多个字段的约束 validator ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30261600/

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