1

我想将 {this} 之类的内容作为休眠验证器验证消息的消息值。

所以我在属性文件中转义了这个给定消息的值:\{this\}

但是 Hibrnate Validator 会尝试检查是否有给定密钥的消息。我认为这是一个错误,但也许我做错了什么?(使用 hibernate-validator 4.3.1 和 5.0.1 测试 - 结果相同 - 堆栈溢出

测试用例:

(为简化起见,它没有属性文件,我只是从 java 类返回值。实际上,handleGetObject 会从我的自定义提供的属性中读取值):

import org.hibernate.validator.HibernateValidator;
import org.hibernate.validator.HibernateValidatorConfiguration;
import org.hibernate.validator.internal.engine.constraintvalidation.ConstraintValidatorFactoryImpl;
import org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator;
import org.hibernate.validator.spi.resourceloading.ResourceBundleLocator;
import org.junit.Test;

import javax.validation.*;
import javax.validation.constraints.Max;
import java.util.Enumeration;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.Set;


public class HibValTest {

@Test
public void shouldNotSubstituteEscaped() throws Exception {

    MessageInterpolator interpolator = new ResourceBundleMessageInterpolator( new ResourceBundleLocator() {

        @Override
        public ResourceBundle getResourceBundle(Locale locale) {
            return new ResourceBundle() {

                @Override
                protected Object handleGetObject(String key) {
                    return "\\{escapedKey\\}";
                }

                @Override
                public Enumeration<String> getKeys() {
                    throw new UnsupportedOperationException();
                }
            };
        }
    }, false);

    HibernateValidatorConfiguration config = Validation.byProvider( HibernateValidator.class ).configure();
    config = config.messageInterpolator(interpolator).constraintValidatorFactory(new ConstraintValidatorFactoryImpl());

    ValidatorFactory factory = config.buildValidatorFactory();
    Validator validator = factory.getValidator();

    Set<ConstraintViolation<A>> validate = validator.validate(new A());
}

private class A {
    @Max(value=1, message = "{key}")
    private int a=2;
}
}

结果:

at org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator.resolveParameter(ResourceBundleMessageInterpolator.java:244)
at org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator.interpolateBundleMessage(ResourceBundleMessageInterpolator.java:211)
at org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator.resolveParameter(ResourceBundleMessageInterpolator.java:244)
at org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator.interpolateBundleMessage(ResourceBundleMessageInterpolator.java:211)
at org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator.resolveParameter(ResourceBundleMessageInterpolator.java:244)
at org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator.interpolateBundleMessage(ResourceBundleMessageInterpolator.java:211)
at org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator.resolveParameter(ResourceBundleMessageInterpolator.java:244)
at org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator.interpolateBundleMessage(ResourceBundleMessageInterpolator.java:211)
4

1 回答 1

0

这看起来确实像一个错误。你能在我们的问题跟踪器中创建一张票吗?

目前,您可以通过不为转义键提供值(如果在您的情况下可能的话)来解决此问题,例如:

protected Object handleGetObject(String key) {
    return key.equals("key") ? "\\{escapedKey\\}" : null;
}
于 2013-05-21T10:35:27.243 回答