f-angi
2/24/2016 - 8:17 AM

Spring validation: manually validating a single object

Spring validation: manually validating a single object

<bean id="messageSource"
	class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
	<property name="basename" value="classpath:spring-messages" />
	<property name="defaultEncoding" value="UTF-8" />
</bean>
@Autowired
ApplicationContext applicationContext;
…
BeanPropertyBindingResult errors = new BeanPropertyBindingResult(object, "object");
validator.validate(object, errors);
for (ObjectError objectError : errors.getAllErrors()) {
    System.out.println(applicationContext.getMessage(objectError, Locale.getDefault()));
}