RESTEasy Bean Validation using Hibernate Validator Provider

The Bean Validation API (JSR-303) defines a meta-data model and API for bean validation based on annotations. The functionality is achieved through the resteasy-hibernatevalidator-provider component. In order to integrate, we need to add resteasy-hibernatevalidator-provider.jar and hibernate-validator.jar files to the classpath.

If you are using maven the, you need to add only below dependency. Change the version number of RESTEasy with yours.


Step 1) Add the validation specific annotations in API parameters

You can use various annotations for validating the request parameters and form input. For example, I have used @NotNull and @Size annotations. Read about all available annotations in link. No not forget to add @ValidateRequest annotation on method API. @ValidateRequest enables the validation on method where it is applied.


import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import org.jboss.resteasy.spi.validation.ValidateRequest;

public class UserService 
	public Response addUser
			@FormParam("firstName") String firstName  , 
			@FormParam("lastName") String lastName
		return Response.ok().entity("User "" + firstName + " " + lastName + "" added through JAX-RS JavaScript API").build();

Step 2) Add validation exception handler

Well this is important step because hibernate validator plugin does not have any facility for exception handling, so you need to embed yous. Exception handlers in RESTEasy are used using the ExceptionMapper class.

package com.howtodoinjava.exception;


import org.hibernate.validator.method.MethodConstraintViolationException;

public class ValidationExceptionHandler implements ExceptionMapper<MethodConstraintViolationException> 
	public Response toResponse(MethodConstraintViolationException exception) 
		return Response.status(Status.BAD_REQUEST).entity("Fill all fields").build();

Step 3) Build the client code and test application

I have written a normal HTML form which will send two parameters for firstName and lastName parameters using form submission. Parameter validation is performed automatically and if validation failed, exception handler is invoked to return correct status code.

		<h1>RESTEasy Hibernate Validator Plugin</h1>
		<div id="error"></div>
		<form method="post" action="./rest/users">
			<p>First Name : <input type="text" name="firstName" id="firstName"/></p>
			<p>LastName : <input type="text" name="lastName" id="lastName"/></p>
			<input type="submit" value="Add User" />
		Demo by : <b>//</b>

You can test the above application either in browser window or you can use some REST testing tools like RESTClient firefox plugin.

RESTEasy bean validation using hibernate validator
RESTEasy bean validation using hibernate validator

To Download the sourcecode of this demo, follow below link.

Sourcecode Download

Happy Learning !!


Notify of
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.