Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resteasy Bean Validation Not Being Invoked

Problem Background

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.

Question

  1. How do I enable bean validation in Resteasy?

What I've Tried

I have done the following:

  1. Annotated my resource class with @ValidateRequest
  2. Annotated the method parameter with @Valid
  3. Annotated the constraints on my input class.
  4. Added a dependency on resteasy-hibernatevalidator-provider

Resource:

@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.
    }
}

User POJO:

@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
}

POM Entry for Validation:

    <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.


Update (6/18/2013):

I reverted the Resteasy version in my pom to 2.3.5.Final and bean validation started working without any code changes.

like image 897
gregwhitaker Avatar asked Jun 09 '13 00:06

gregwhitaker


1 Answers

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'.

like image 61
shlomi33 Avatar answered Sep 21 '22 04:09

shlomi33