I have a Resteasy service that uses Spring through Resteasy's SpringContextLoaderListener
. This is built on Resteasy version 3.0-beta-6
.
I would like to use bean validation on the incoming requests, but I can not get Resteasy to call the validator. It acts like there is no validation configured and simply passes the method the invalid input object.
I have done the following:
@ValidateRequest
@Valid
resteasy-hibernatevalidator-provider
@Named
@Path("users")
@ValidateRequest
public class UserResource
{
/**
*
* @param user
*
* curl -x POST http://localhost:7016/api/1.0/users
*
*/
@POST
@Consumes({MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_JSON})
public Response createUser(@Valid User user)
{
//User creation logic here.
}
}
@JsonPropertyOrder({
"user_id",
"user_name",
"email"
})
public class User
{
@JsonProperty("user_id")
private Long userId;
@JsonProperty("user_name")
@NotNull(message = "Username must be provided")
private String username;
@Email(message = "Invalid email address.")
private String email;
//Getters and Setters Removed for Brevity
}
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-hibernatevalidator-provider</artifactId>
<version>${resteasy.version}</version>
</dependency>
The resteasy-hibernatevalidator-provider
dependency brings in the HibernateValidatorContextResolver
and its associated HibernateValidatorAdapter
.
I reverted the Resteasy version in my pom to 2.3.5.Final
and bean validation started working without any code changes.
Running with Resteasy '3.0.6.Final' and Spring '4.1.0.RELEASE'.
The 'resteasy-hibernatevalidator-provider' does not evaluate the @Valid annotated params. Using the 'resteasy-validator-provider-11' makes everything work and as a bonus is using Hiberbate validator '5.0.1.Final' instead of needing a Hibernate validator version 4 when using the 'resteasy-hibernatevalidator-provider'.
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