1

I'm using the following class to validate my beans with Hibernate Validation. It works great. It's basically the listener found here for JPA 1, converted to use to validate beans in my Spring controllers. I'm attempting to customize the messages returned. This and every other resource I can find say to just put a ValidationMessages.properties file in the Web-Inf directory. This file is also included below. This is not working. Any suggestions?

public class BeanValidationTool {

    public void validate(Object entity) {
        TraversableResolver tr = new MyTraversableResolver();
        Validator validator = Validation.buildDefaultValidatorFactory().usingContext().traversableResolver(tr).getValidator();
        final Set<ConstraintViolation<Object>> constraintViolations = validator.validate(entity);
        if (constraintViolations.size() > 0) {
          Set<ConstraintViolation<?>> propagatedViolations = new HashSet<ConstraintViolation<?>>(constraintViolations.size());
          Set<String> classNames = new HashSet<String>();
            for (ConstraintViolation<?> violation : constraintViolations) {
              propagatedViolations.add(violation);
              classNames.add(violation.getLeafBean().getClass().getName());
            }
            StringBuilder builder = new StringBuilder();
            builder.append("validation failed for classes ");
            builder.append(classNames);
            throw new ConstraintViolationException(builder.toString(), propagatedViolations);
        }
    }

    public class MyTraversableResolver  implements TraversableResolver {

        public boolean isReachable(Object traversableObject, Path.Node traversableProperty, Class<?> rootBeanType, Path pathToTraversableObject, ElementType elementType) {
            return traversableObject == null || Hibernate.isInitialized(traversableObject);
        }

        public boolean isCascadable(Object traversableObject, Path.Node traversableProperty, Class<?> rootBeanType, Path pathToTraversableObject, ElementType elementType) {
            return true;
        }
    }
  }`

ValidationMessages.properties file

validator.min = must be greater than or equal to {value}

validator.notEmpty = This field can't be empty

1 Answer 1

1

You have to define messageSource in your application context:

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource" abstract="false"
      scope="singleton" lazy-init="default">
    <property name="basename" value="ValidationMessages"/>
</bean>

Than you just use the bean to retrieve the messages:

@Autowired
private MessageSource messageSource;

public String getMessage(String messageName) {
    return messageSource.getMessage(messageName, null, null);
}

In order to access the beans in context you will have to do this as well:

@Component
public class BeanValidationTool{...
Sign up to request clarification or add additional context in comments.

1 Comment

This is a great description of how to set up custom messages in Spring but, maybe my understanding is limited, I think that in order to customize Hibernate Validator messages specifically, that I should not have to use a bean. Instead the values should be substituted automatically and be returned when the validate method is called.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.