Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the proper way to validate requests with Resteasy?

I use Resteasy in combination with Google Guice using Resteasy-Guice. I have been looking for ways to validate my request bodies. I want to do for example:

public static class MyPojo {

   @NotEmpty private String contents;

}

And then use in my resource

@POST
@ValidateRequest
public void doPost(@Valid MyPojo myPojo) {
   // use myPojo only if valid
}

I have been working with the resteasy-hibernate-validator-provider. But since I switched to newer versions, this introduced the (unwanted?) dependency to EJB. See also: RESTEASY-1056. In the comments is stated that you should switch to the newer validator-11 instead:

Switch to resteasy-validator-provider-11, which implements the newer Bean Validation 1.1 specification.

The docs say:

Validation is turned on by default (assuming resteasy-validator-provider-11-.jar is available), though parameter and return value validation can be turned off or modified in the validation.xml configuration file. See the Hibernate Validator documentation for the details.

I however do not manage to get this working to my configuration, because I find myself including dependencies like hibernate-validator, javax.el-api, javax.el and hibernate-validator-cdi and annotations like ValidateOnExecution. I however do not find any of this being instantiated or invalid requests being rejected.

What is the preferred, lightweight, and working way to do validation with Resteasy?

like image 888
Jan-Willem Gmelig Meyling Avatar asked Jun 20 '15 17:06

Jan-Willem Gmelig Meyling


2 Answers

You do not have to specify any annotation on the resource itself or do additional configuration. Just the constraint annotations on POJOs are enough get it working.

My setup is as follows:

The resource method:

@POST
public void doPost(@Valid MyPojo myPojo) {
   // use myPojo only if valid
}

The POJO:

public static class MyPojo {
   @NotEmpty private String contents;
}

Tested with the following dependencies:

javax.validation version 1.1.0.Final

resteasy-validator-provider-11 version 3.0.11.Final

hibernate-validator version 5.0.0.Final and 5.0.1.Final

like image 175
Thomas Smith Avatar answered Nov 19 '22 18:11

Thomas Smith


  • I accidentally had a transitive dependency to the hibernate-validator-provider which caused previous tries to fail. Ensure that you do not have a transitive dependency to the hibernate-validator-provider. For me this caused the following exception: issues.jboss.org/browse/RESTEASY-826 .
  • Based on Thomas answer I added dependencies to javax.validation, resteasy-validator-provider-11, hibernate-validator.
  • Then I still received exceptions (java.lang.NoClassDefFoundError: javax/el/PropertyNotFoundException). Based on this answer I added javax.el-api and el-impl as dependencies. I think this is because I use an embedded servlet container.
  • I had to remove the @ValidateOnRequest annotation on the resources, they are not necessary anymore

Final working configuration:

    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>1.1.0.Final</version>
    </dependency>
    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-validator-provider-11</artifactId>
        <version>3.0.11.Final</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>5.0.1.Final</version>
    </dependency>
    <dependency>
        <groupId>javax.el</groupId>
        <artifactId>javax.el-api</artifactId>
        <version>3.0.0</version>
    </dependency>
    <dependency>
        <groupId>org.glassfish.web</groupId>
        <artifactId>el-impl</artifactId>
        <version>2.2</version>
    </dependency>
like image 5
Jan-Willem Gmelig Meyling Avatar answered Nov 19 '22 18:11

Jan-Willem Gmelig Meyling