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?
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
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 .javax.validation
, resteasy-validator-provider-11
, hibernate-validator
.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.@ValidateOnRequest
annotation on the resources, they are not necessary anymoreFinal 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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With