0
我想把{this}這樣的東西作爲hibernate驗證器驗證消息的消息值。如何在hibernate驗證器消息中提供花括號(提供測試用例)
,所以我逃過屬性此給定的消息的值文件:\ {此\}
然而Hibrnate驗證程序嘗試,以檢查是否存在給定密鑰的消息。 我認爲這是一個錯誤,但也許我做錯了什麼? (與Hibernate驗證器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)
提交https://hibernate.atlassian.net/browse/HV-798 –
爲您解決辦法的建議 - 這是目前不方便對我們來說,我們返回「??? ???關鍵「對於缺少的鍵來查看屬性中缺少某些內容。 –