1

My question is similar to Resteasy Bean Validation Not Being Invoked. The solutions there don't work, though.

I'm using Resteasy 3.0.9.Final with resteasy-validator-provider-11 in my pom. I'm launching the whole thing using a custom Jetty class.

Weirdly, validation is working fine on @PathParams, but not on beans.

@POST
@Path("/foo/{myParam}")
public Message post(MyBean myBean, @PathParam("myParam") @Size(min=5) String myParam) {
    return new Message("bean:" + myBean.toString());
}

public static class MyBean {

    @NotNull
    public String myStr;

    @Max(value = 3)
    public int myInt;

    public String toString() {
        return myStr + myInt;
    }
}

In this case, the @Size constraint on myParam is working fine. But the @NotNull and @Max constraints in MyBean are not getting invoked.

Am I missing an annotation somewhere?

Here's one more clue. My logs include these entries:

2014-12-30 12:16:56 org.hibernate.validator.internal.util.Version 6446 INFO  HV000001: Hibernate Validator 5.0.1.Final
2014-12-30 12:16:56 org.jboss.resteasy.plugins.validation.AbstractValidatorContextResolver 6477 INFO  Unable to find CDI supporting ValidatorFactory. Using default ValidatorFactory
Community
  • 1
  • 1
ccleve
  • 15,239
  • 27
  • 91
  • 157

1 Answers1

2

I believe, but not 100% sure, that the issue is that you're missing @Valid on the MyBean parameter. I would also recommend to make it a separate class, rather than a static class.

Per the spec, validation constraints on methods where the object is a complex object need to have the parameter annotated @Valid to ensure that the constraints are cascaded.

John Ament
  • 11,595
  • 1
  • 36
  • 45
  • Bingo, that was it. Contrary to the Resteasy docs, bean validation is not turned on by default. (BTW, using a static class seems to be ok.) – ccleve Dec 31 '14 at 20:33
  • What do you mean not turned on by default? The `@Valid` requirement is from the bean val spec. – John Ament Dec 31 '14 at 20:55
  • http://docs.jboss.org/resteasy/docs/3.0.9.Final/userguide/html_single/index.html#d4e2497 Granted, it's a bit ambiguous what they mean by "turned on by default". – ccleve Dec 31 '14 at 22:49
  • I think what you need to review is http://docs.jboss.org/hibernate/validator/5.0/reference/en-US/html/chapter-method-constraints.html#section-declaring-method-constraints . Also by "they" that includes me since I'm a contributor for resteasy, and what is meant there is that we will continue to delegate down to the validator for properly annotated methods, arguments and return values. – John Ament Dec 31 '14 at 23:17