views:

1593

answers:

3

Hi,

What could the best strategy for writing validation layer for mid-enterprise level business application built on Spring 2.5

I know that Spring provides facility where we can implement Validator interface and write validation logic in validate method. But this will be restricted to only web requests coming through spring controller.

I would like to develop the validation framework which can be utilized during web-services calls.

In other words, the framework can remain and be called independently without the need of implementing Validator interface and then too it can be automatically integrated into Spring MVC flow.

Hope you get my point.

+2  A: 

The Spring Validation framework can be used outside of Spring MVC. What WebServices Stack are you using? If you are using Spring-WS (Spring's Web Services stack) they have special instructions on how to set up the validator here:

http://static.springframework.org/spring-ws/sites/1.5/reference/html/server.html#d0e2313

If you are using some other stack, it is probably easier to implement something for that stack (or find one) that will use Spring's validation framework.

Chris Dail
A: 

Thanks kilhoffer for your idea, well I am not acquainted with AOP so may be I need to first learn it :)

Chris, your idea seems better but I was planning to develop WS using CXF. I am not aware how we can use spring-ws for developing WS. If you have any thoughts about CXF vs SPRING-WS please let me know.

jatanp
A: 

Recall that the Validator interface defines two methods:

boolean supports(Class clazz)
void validate(Object target, Errors errors)

The "Object target" is your form object, which is the whole object representing the page to be shown to the user. The Errors instance will contain the errors that will be displayed to the user.

So, what you need to do is define an intermediary that can be called with the specifics in your form that you want to validate which are also the same as in your web service. The intermediary can take one of two forms:

  1. (probably the best):

    public interface ErrorReturning { public void getErrors(Errors errors); }

  2. (this can get ugly really fast if more than two states are added):

    public interface ValidationObject { public Errors getErrors(Errors errors); public Object getResultOfWebServiceValidation(); }

I would suggest that the first approach be implemented. With your common validation, pass an object that can be used for web service validation directly, but allow it to implement the getErrors() method. This way, in your validator for Spring, inside your validation method you can simply call:

getCommonValidator().validate(partialObject).getErrors(errors);

Your web service would be based around calls to getCommonValidator().validate(partialObject) for a direct object to be used in the web service.

The second approach is like this, though the interface only allows for an object to be returned from the given object for a web service validation object, instead of the object being a usable web service validation object in and of itself.

MetroidFan2002