users@jersey.java.net

Re: [Jersey] Re: Custom Invalid Parameter Handling

From: Chris Carrier <ctcarrier_at_gmail.com>
Date: Wed, 14 Apr 2010 15:52:53 -0700

Yep I do exactly that in our app. I have a snippet something like:

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 HttpRequestValidationException(errorMessage);
        }

Because the validator will automatically return an error field and
error reason that you can turn into explicit error messages. Plus it
returns all the failed validation reasons and not just the first one.
Then I have a Jersey ExceptionMapper that catches all of my domain
HTTP exceptions and turns them into JSON, logs stuff and does some
other convenience stuff. Because of the way my app is structured I
have this validation step implemented in one place only and
implementations inherit the behavior. So all i do is annotate my
domain classes and they're automatically validated and the error is
returned in JSON etc. I'm not sure if this works when you have a
bunch of custom validation stuff but it was exactly what I was looking
for after the 1000th time writing a 'validatePostRequest' method.

Chris

On Wed, Apr 14, 2010 at 3:44 PM, Dan Forward <dan-nabble_at_forwardhome.com> wrote:
>
> Using Hibernate Validator is an intriguing idea. I did not know JSR 303
> existed. However, I think I would still be left with the challenge that
> Jersey still would return 400 Bad Request for any invalid parameter without
> any indication of which parameter was invalid. Even simple things, like
> Gender, which we implement with an Enum, we want to catch and return a JSON
> message with the 400 Bad Request.
>
> Sincerely,
>
> Dan Forward
> --
> View this message in context: http://n2.nabble.com/Custom-Invalid-Parameter-Handling-tp4904335p4904542.html
> Sent from the Jersey mailing list archive at Nabble.com.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>
>