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.

<dependency>
   <groupId>org.jboss.resteasy</groupId>
   <artifactId>resteasy-hibernatevalidator-provider</artifactId>
   <version>2.3.1.GA</version>
</dependency>


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.

package com.howtodoinjava.rest;
 
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;
 
import org.jboss.resteasy.spi.validation.ValidateRequest;
 
@Path("/rest")
public class UserService 
{
    @Path("/users")
    @POST
    @ValidateRequest
    public Response addUser
    (
            @NotNull
            @Size(min=1,max=50) 
            @FormParam("firstName") String firstName  , 
             
            @Size(max=50)
            @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 ExceptionMapperclass.

package com.howtodoinjava.exception;
 
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
 
import org.hibernate.validator.method.MethodConstraintViolationException;
 
@Provider
public class ValidationExceptionHandler implements ExceptionMapper<MethodConstraintViolationException> 
{
    @Override
    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.

<html>
    <body>
        <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" />
        </form>
        Demo by : <b>http://www.howtodoinjava.com</b>
    </body>
</html>


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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值