However, one piece of missing functionality was the ability to also validate method calls. For example, wouldn't it be nice if you could use normal JSR 303 annotations to safeguard business layer calls as well?
Of course you can enforce constraints programmatically, e.g. prevent the passing of Null parameters. Spring for instance provides the org.springframework.util.Assert class. However, it would be desirable, if constraints placed on input parameters as well as return values were part of the method contract.
Enter Hibernate Validator 4.2, which was released in June of 2011. It provides now method validation and Gunnar Morling, one of the committers, has an excellent write-up on this on his blog at:
Also, check out the Hibernate validator documentation regarding method validation. Gunnar Morling also provides some preliminary Spring support available at GitHub:
But the news for Spring aficionados is getting even better - Direct support for Method Validation With Hibernate Validator is now available in the lastest Spring 3.1 release that went GA just two weeks ago.
Specifically Spring 3.1 provides the following support in regards to method validation:
- org.springframework.validation.beanvalidation.MethodValidationInterceptor
- org.springframework.validation.beanvalidation.MethodValidationPostProcessor
@Validated public interface BusinessService { @NotNull String convertToUpperCase(@NotEmpty String input); }The @Validated Annotation indicates that the methods in the class are to be included for method validation. Keep in mind that you can specify your own annotation that will be enabling method level validation for the annotated class. Specify your own annotation by calling setValidatedAnnotationType on the MethodValidationPostProcessor.
However, placing the annotations themselves will not enforce the constraints 'automagically'. In fact Spring will be using AOP (Aspect Oriented Programming) Advices under the covers that delegate to Hibernate Validator. The easiest way to enable these advices is to declare a MethodValidationPostProcessor bean in your Spring application context as shown below.
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.hillert.spring.validation" /> <bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/> <bean class="org.springframework.validation.beanvalidation.MethodValidationPostProcessor"/> </beans>For more details, I have published a small project over at GitHub that illustrates method level validation using Hibernate Validator:
In order to get get started:
- Clone the repository using $ git clone git://github.com/ghillert/spring-hibernate-validator-sample.git
- $ cd spring-hibernate-validator-sample
- Build the sample using Maven $ mvn clean package
- Run it with $ mvn exec:java
- The input String must not be empty (not null and not an empty String)
- The conversion service will never return null
- Pressing just enter (no string entered) will cause an empty String parameter to be set
- Submitting "null" (The String), will cause a null parameter to be set
- When entering "returnnull", the underlying business method will return Null
Lastly, the current method validator support via Spring is specifically targetting Hibernate Validator. However, there are standardization efforts underway as part of the Bean Validation 1.1 specification (JSR-349). In order to follow the progress, please follow also http://beanvalidation.org/.
Spring will provide support for JSR-349 as soon as the official standard has been ratified. In order to track inclusion into the Spring framework, please follow also Spring Framework Jira ticket SPR-8199 . As of today this feature should be available in the next Spring 3.2 release.
I hope this blog entry and the accompanying sample have showed you how easy it is to get started with method level validation using Spring and Hibernate Validator. Please let me know if you have further questions or run into issues.