How to get default validation messages from Hibernate Validator?

I am trying to get a default validation error message using MessageSource. The code I'm working with uses reflection to get the value of a parameter message. With a constraint that does not cancel the parameter message, I would like to receive a default error message. When I call a method messagein a validation annotation, I get {org.hibernate.validator.constraints.NotBlank.message}(for example, for annotation @NotBlank). Then I tried to use MessageSourceto get the error message as follows:

String message = messageSource.getMessage(key, null, Locale.US);

I tried to set keyto {org.hibernate.validator.constraints.NotBlank.message}, org.hibernate.validator.constraints.NotBlank.message(removed curly braces) and even org.hibernate.validator.constraints.NotBlank, but I keep getting null. What am I doing wrong here?

UPDATE

Clarification. I get the impression that Spring comes with a message.propertiesdefault file for its limitations. Am I correct in this assumption?

UPDATE

Changing the name of the question to better reflect what I was trying to do.

+2
source share
1 answer

After going to a blog post from one of the Hibernate guys, and after he started working at the Validator Validator source, I think you get the idea:

public String getMessage(Locale locale, String key) {
    String message = key;
    key = key.toString().replace("{", "").replace("}", "");

    PlatformResourceBundleLocator bundleLocator = new PlatformResourceBundleLocator(ResourceBundleMessageInterpolator.DEFAULT_VALIDATION_MESSAGES);
    ResourceBundle resourceBundle = bundleLocator.getResourceBundle(locale);

    try {
       message = ResourceBundle.getString(key);
    }

    catch(MissingResourceException ) {
       message = key;
    }

    return message;
}

, PlatformResourceBundleLocator . ResourceBundle . , . ; , , .

UPDATE

( ) - applicationContext.xml :

<bean id="resourceBundleSource" class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basenames">
        <list>
            <value>org.hibernate.validator.ValidationMessages</value>
        </list>
    </property>
</bean>

MessageSource , messageSource.getMessage(). , , .

+1

All Articles