Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何为列表创建ConstraintValidator_Java_Hibernate_List_Bean Validation - Fatal编程技术网

Java 如何为列表创建ConstraintValidator

Java 如何为列表创建ConstraintValidator,java,hibernate,list,bean-validation,Java,Hibernate,List,Bean Validation,我有一个简单的验证器来验证字符串值是否是预定义列表的一部分: public class CoBoundedStringConstraints implements ConstraintValidator<CoBoundedString, String> { private List<String> m_boundedTo; @Override public void initialize(CoBoundedString annotation) { m_boun

我有一个简单的验证器来验证字符串值是否是预定义列表的一部分:

public class CoBoundedStringConstraints implements ConstraintValidator<CoBoundedString, String>
{

private List<String> m_boundedTo;

@Override
public void initialize(CoBoundedString annotation)
{
    m_boundedTo = FunctorUtils.transform(annotation.value(), new ToLowerCase());
}

@Override
public boolean isValid(String value, ConstraintValidatorContext context)
{
    if (value == null )
    {
        return true; 
    }

    context.disableDefaultConstraintViolation();
    context.buildConstraintViolationWithTemplate("should be one of " + m_boundedTo).addConstraintViolation();
    return m_boundedTo.contains(value.toLowerCase());
}

}
我想为字符串列表创建一个验证器,以验证如下内容:

@CoBoundedString({"a","b" })
public List<String> operations = new ArrayList<String>();
@CoBoundedString({“a”,“b”})
public List operations=new ArrayList();
我试过这个:

public class CoBoundedStringListConstraints implements ConstraintValidator<CoBoundedString, List<String>>
{

private CoBoundedString m_annotation;

@Override
public void initialize(CoBoundedString annotation)
{
    m_annotation = annotation;
}

@Override
public boolean isValid(List<String> value, ConstraintValidatorContext context)
{
    if (value == null )
    {
        return true; 
    }

    CoBoundedStringConstraints constraints = new CoBoundedStringConstraints();
    constraints.initialize(m_annotation);
    for (String string : value)
    {
        if (!constraints.isValid(string, context))
        {
            return false;
        }
    }
    return true;
}

}
公共类CoBoundedStringListConstraints实现ConstraintValidator
{
私有CoBoundedString m_注释;
@凌驾
公共void初始化(CoBoundedString注释)
{
m_注释=注释;
}
@凌驾
公共布尔值有效(列表值、ConstraintValidatorContext上下文)
{
如果(值==null)
{
返回true;
}
CoBoundedStringConstraints=新的CoBoundedStringConstraints();
约束。初始化(m_注释);
for(字符串:值)
{
如果(!constraints.isValid(字符串、上下文))
{
返回false;
}
}
返回true;
}
}

问题是,如果列表包含2个或多个非法值,则只有一个(第一个)约束冲突。我希望它有不止一个。我应该怎么做?

您当前的代码有两个问题:

在您的
CoBoundedStringListConstraints
isValid
方法中,您应该像这样迭代给定列表的所有元素(适当设置
allValid
标志):


我找到了一个可能的解决方案:。但是,我没能成功。谢谢!它起作用了
@Valid
假设执行
CoBoundedStringListConstraints
的操作?
public class CoBoundedStringListConstraints implements ConstraintValidator<CoBoundedString, List<String>>
{

private CoBoundedString m_annotation;

@Override
public void initialize(CoBoundedString annotation)
{
    m_annotation = annotation;
}

@Override
public boolean isValid(List<String> value, ConstraintValidatorContext context)
{
    if (value == null )
    {
        return true; 
    }

    CoBoundedStringConstraints constraints = new CoBoundedStringConstraints();
    constraints.initialize(m_annotation);
    for (String string : value)
    {
        if (!constraints.isValid(string, context))
        {
            return false;
        }
    }
    return true;
}

}
@Override
public boolean isValid(List<String> value,
        ConstraintValidatorContext context) {
    if (value == null) {
        return true;
    }

    boolean allValid = true;
    CoBoundedStringConstraints constraints = new CoBoundedStringConstraints();
    constraints.initialize(m_annotation);
    for (String string : value) {
        if (!constraints.isValid(string, context)) {
            allValid = false;
        }
    }
    return allValid;
}
@Override
public boolean isValid(String value, ConstraintValidatorContext context) {

    if (value == null) {
        return true;
    }

    if (!m_boundedTo.contains(value)) {
        context.disableDefaultConstraintViolation();
        context.buildConstraintViolationWithTemplate(
                value + " should be one of " + m_boundedTo)
                .addConstraintViolation();
        return false;
    }
    return true;
}