Home > database >  How to enable @Valid annotation with REST Easy and Vertx
How to enable @Valid annotation with REST Easy and Vertx

Time:09-27

I am trying to get the @Valid annotation working in a project that is using RESTEasy and Vert.x.

I have an API controller setup with a method signature like public Future<SomeResponseDto> someController(@Valid SomeRequestDto dto) where SomeRequestDto looks like the following:

@Value
public class SomeRequestDto {

  @NotEmpty
  String someField;
  
  @JsonCreator
  public SomeRequestDto(@JsonProperty("some_field") String someField) {
    this.someField = someField;
  }
  
}

My expectation is that when I call someController with some JSON that doesn't meet the validation criteria (namely that someField is "") an error is returned and the body of someController is not executed. This is not the case and the body of someController is being executed.

Interestingly, if I do manual validation in the body of someController I do get a Set containing the validation error I'd expect. Manual validation uses the following code:

ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
Validator validator = factory.getValidator();

final var result = validator.validate(dto); // This holds the expected constraint validation error

In terms of dependencies, my project depends on the following directly (listed in the build.gradle):

org.jboss.resteasy:resteasy-validator-provider-11:3.0.11.Final
| org.hibernate:hibernate-validation:5.0.1.Final // Pulled in via the above

And depends on the following transitively (via dependencies of dependencies):

org.jboss.resteasy:resteasy-vertx:4.5.3.Final
| org.jboss.resteasy:resteasy-client:4.5.3.Final // Pulled in as a dependency of the above transient depdency

Note: | indicates a child dependency (dependency of a dependency or a dependency of a transitive dependency).

Finally, I have tried following the instructions listed here to no avail (expect adding validation.xml since I don't think this is required). Any help would be appreciated!

CodePudding user response:

As @JamesR.Perkins suggested, this was due to using an older version of the validation provider resteasy-validator-provider compared to resteasy-vertx. Additionally, I was using the old artifact ID.

To get this working I removed org.jboss.resteasy:resteasy-validator-provider-11:3.0.11.Final from my build.gradle and added org.jboss.resteasy:resteasy-validator-provider:4.5.3.Final. Note the artifact ID has updated from resteasy-validator-provider-11 to resteasy-validator-provider and I chose version 4.5.3.Final for the validator since it matched the version of resteasy-vertx I was using.

For clarity, my project now depends on the following directly (listed in the build.gradle):

org.jboss.resteasy:resteasy-validator-provider:4.5.3.Final
| org.hibernate:hibernate-validation:6.2.0.Final // Pulled in via the above

And depends on the following transitively (via dependencies of dependencies):

org.jboss.resteasy:resteasy-vertx:4.5.3.Final
| org.jboss.resteasy:resteasy-client:4.5.3.Final // Pulled in as a dependency of the above transient depdency

Note: | indicates a child dependency (dependency of a dependency or a dependency of a transitive dependency).

  • Related