RESTFul validation using Spring Part 1

Since last couple of week I was trying to figure “How RESTFul validation should be done in Spring”. Also how to approach exception handling we do not want to mandate Spring MVC usage at as view layer”. There is not enough article over internet on approach. I can see a lot of developer seeking information/help from Spring forum but there is long silence over this topic in forum.

Finally I decided to evaluate couple of approach to nail down above stated problem and its benefit/issue. I was able implement Custom RESTFul validation using spring validator as core. In blog I will discuss one possible approach. I would welcome your comment and better approach to achieve this feature.

When to use Custom Validation?

In case you have simple validation requirement (I.e. fieldName is required, Data validations etc) you can use hibernate annotation validation (@Valid). For complex validations as data validation with database/third-party, data validation for complex calculation, data comparison etc you need to have custom validation.

How to achieve RESTFul Custom Validation?

Spring offers a design for validation (and data binding). Data binding is useful for allowing user input to be dynamically bound to the domain model of an application (or whatever objects you use to process user input). Spring provides the so-called DataBinder to do exactly that. The Validator and the DataBinder make up the validation package. If you are new to Spring Validation read

To achieve custom validation I used Spring AOP (Before) at controller layer.

Example: User can register within application through RESTFul service. We want to have custom validation and throw custom Error messages (messageCode, messageDetail etc).

At controller:

@RequestMapping(value = "/user/register", method = RequestMethod.POST)
public User registerUser(@RequestBody @Valid User user) {
       return user;

Here custom annotation @Validators(validators=”myapplication.registeration.user”) will do the trick for us. Using AOP you can define before advice and point cut expression at controller method. This will call ValidationAspect where we can invoke custom validator dynamically.


                                   <aop:pointcut  id=”serviceMethodPointcut” 
                                               expression=”execution(* *(..)) and @annotation(com.validation.Validators)”/>
           <!– Aspect that implements validating the parameters of the service method
                   and should be applied to all service methods –>
                                   <aop:aspect ref=”validationAspect”>
                                               <aop:before method=”validate” pointcut-ref=”serviceMethodPointcut”/>


           beanid=”validationAspect”  class=”com.validation.ValidationAspect”> 

Core program: We will create annotation interface Validators where application developer can add validators. ValidatorAspect class validate method is called whenever Validators annotation is encountered at controller layer. ValidatorAspect class will have logic to dynamically invoke custom validator. Next


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: