users@jersey.java.net

[Jersey] bean validation + mvc

From: Trenton D. Adams <trenton.d.adams_at_gmail.com>
Date: Sun, 21 Feb 2016 18:53:52 -0700

Good day,

Is there a way of getting bean validation to play nicely with the MVC
features? Namely I'd like the bean validation to be passive validation,
and let me handle how to display the errors.

Obviously I don't just want an http 400 with an error message, for
example. I would like to return the same page again, with all the errors
noted on it.

It doesn't appear, based on the bean validation documentation, that this is
possible unless I were to write my own validation for each field. It seems
that Jersey's bean validation support was built around the concept of web
services only, where the client will receive an error. I suppose I could
work with that, by making my entire application AJAX based, with errors
coming back through the call, but that's not ideal.

As an example, this works...

public class PassiveValidator
    implements ConstraintValidator<PassiveValidate, String>
{
    @Context
    private HttpServletRequest request;

    @Override
    public void initialize(final PassiveValidate constraintAnnotation)
    {
    }

    @Override
    public boolean isValid(final String value,
        final ConstraintValidatorContext context)
    {
        request.setAttribute("fail", value == null || "".equals(value.trim()));
        return true;
    }
}

I tried setting...

property(ServerProperties.RESOURCE_VALIDATION_IGNORE_ERRORS, true);

But that doesn't actually do anything, I still get a 400 with no message,
when I annotate a field variables with @NotNull(message = "error"), and
then don't pass the query parameter in.

Thanks.