users@jersey.java.net

Re: [Jersey] Jersey and JSR303

From: Chris Carrier <ctcarrier_at_gmail.com>
Date: Thu, 29 Jul 2010 14:01:33 -0700

I use it all plugged into Spring. There is a section in the Spring
docs on validation and Spring provides an abstraction so that
basically you just implement to the javax interface and Spring
automagically detects any implementation of JSR303 in the classpath
and uses that implementation. I then annotate my domain classes with
the validation annotations like @NotNull and @NotEmpty. Supposedly
the validation stuff should understand JPA annotations that I use to
bind to my DB but I couldn't get that to work.

Then I just declare a validator bean and inject it where needed like:

<bean id="validator"
class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"/>

That spring abstraction actually implements two different interfaces
that you can code to. A Spring one or the javax.validation.Validator
standard. I use the standard. Then to validate an annotated class
you can use a snippet like:

protected void validateResource(T obj) throws SomeException {
        Set<ConstraintViolation<T>> violations = validator.validate(obj);
        if (violations != null && violations.size() > 0) {
            String errorMessage = "Request validation failed. Error
message(s): ";
            for (ConstraintViolation cv : violations) {
                errorMessage += String.format("[%1$s %2$s], ",
cv.getPropertyPath(), cv.getMessage());
            }
            throw new SomeException(errorMessage);
        }
    }

Sloppy string appending stuff aside that's all you really need to do.
I have a common endpoint superclass that implements most of my CRUD
type endpoint stuff and then anything that extends that gets the
validation for free. Right now I only really use it for POSTs where
Jersey marshals the objects for me automatically and then they get
validated. For individual GET parameters I never found this to be of
much use.

Anyway if you're using Spring that should be what you need. If not
some of that is useless ;).

Now that I think of it you might be able to get this cleaner by using
a Jersey request filter. Though the rest would still be the same I
would inject the validator etc.

Chris

2010/7/29 Alexandru Popescu ☀ <the.mindstorm.mailinglist_at_gmail.com>:
> On Thu, Jul 29, 2010 at 11:35 PM, Chris Carrier <ctcarrier_at_gmail.com> wrote:
>> Yeah I use the HibernateValidator implementation.  Simplified my
>> validation quite a bit.
>>
>
> Any resources to help us get started with that? I'm pretty sure that
> once you get one of them running you can easily get the other too
> (n.b. I am aware of just two implementations of JSR-303)
>
> thanks in advance,
>
> :- alex
>
>> On Thu, Jul 29, 2010 at 1:07 PM, Brian Moseley <bcm_at_maz.org> wrote:
>>> agreed. I just started looking into how to combine the two myself.
>>>
>>> 2010/7/29 Alexandru Popescu ☀ <the.mindstorm.mailinglist_at_gmail.com>:
>>>
>>>> Has anyone tried to use the 2 together? They seem like a very natural
>>>> fit, so I'm pretty sure I'm not the first one to ask about it.
>>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>>
>>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
>> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>
>