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?
hibernate-validator-provider
which caused previous tries to fail. Ensure that you do not have a transitive dependency to thehibernate-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 addedjavax.el-api
andel-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: