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

<p>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.</p>

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

<p>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.</p>

<p>Hope you get my point.</p>

[{'answer_id': 57422, 'author': 'Chris Dail', 'author_id': 5077, 'author_profile': 'https://Stackoverflow.com/users/5077', 'pm_score': 1, 'selected': False, 'text': '<p>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:</p>\n\n<p><a href="http://static.springframework.org/spring-ws/sites/1.5/reference/html/server.html#d0e2313" rel="nofollow noreferrer">http://static.springframework.org/spring-ws/sites/1.5/reference/html/server.html#d0e2313</a></p>\n\n<p>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.</p>\n'}, {'answer_id': 67287, 'author': 'MetroidFan2002', 'author_id': 8026, 'author_profile': 'https://Stackoverflow.com/users/8026', 'pm_score': 0, 'selected': False, 'text': '<p>Recall that the Validator interface defines two methods:</p>\n\n<pre><code>boolean supports(Class clazz)\nvoid validate(Object target, Errors errors)\n</code></pre>\n\n<p>The <code>Object target</code> 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.</p>\n\n<p>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:</p>\n\n<ol>\n<li><p>(probably the best):</p>\n\n<pre><code>public interface ErrorReturning { \n    public void getErrors(Errors errors);\n}\n</code></pre></li>\n<li><p>(this can get ugly really fast if more than two states are added):</p>\n\n<pre><code>public interface ValidationObject {\n    public Errors getErrors(Errors errors);\n    public Object getResultOfWebServiceValidation();\n}\n</code></pre></li>\n</ol>\n\n<p>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 <code>getErrors()</code> method.  This way, in your validator for Spring, inside your validation method you can simply call:</p>\n\n<pre><code>getCommonValidator().validate(partialObject).getErrors(errors);\n</code></pre>\n\n<p>Your web service would be based around calls to <code>getCommonValidator().validate(partialObject)</code> for a direct object to be used in the web service.</p>\n\n<p>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.</p>\n'}]
['https://Stackoverflow.com/questions/57314', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/959/']