简体   繁体   中英

Hibernate-validator: How do I interpolate messages with variables containing format “${…}”?

I have a hibernate-validation message that interpolates a value ${value}.

Issue is, that the value variable itself is a string of the form "${ohno}". I'd like my final interpolated message to replace ${value} with this literal string "${ohno}" but instead it seems to attempt some sort of recursive interpolation which produces the error "Cannot find property ohno".

I can't really thing of a way to "escape" this string in the message template. Perhaps I can escape it in the string itself before I pass it to the annotation but I'd rather not have to do that (I use this string as a literal elsewhere).

Does anyone know of a work around? Or perhaps I should open up a ticket with hibernate-validator...

There is really no work around except escaping {,},\\ and $ are meta characters which need to be escaped in Bean Validation. See Message Interpolation in the spec. See also HV-934 in the Validator issue tracker.

Now, if you really do not want to interpolate according to the Bean Validation specification, you can write your on MessageInterpolator and make sure that it gets picked up during Validator initialization. Basically you need a validation.xml pointing out your custom interpolator. Check the docs, there are several examples how to do it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM