RestEasy - 参数绑定 - 验证和错误 - EJB [英] RestEasy - Parameters Binding - Validation and Errors - EJB

查看:429
本文介绍了RestEasy - 参数绑定 - 验证和错误 - EJB的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我说我定义一个POJO,其参数传递给一个REST调用

  class MyVO {
@ NotNull
@PathParam(name)
private String name;

@NotNull
@PathParam(age)
private整数年龄;
// getter和setter
}

public class RESTclass {
public postData(@Form MyVO vo){
}
}

它会自动绑定MyVO中的对象。但是我在哪里可以得到验证错误?
它在绑定期间是否触发验证?如果没有,如何触发验证?



Spring做的都很好。它具有可以注入的BindingResult参数。
什么是等价的?



任何想法?

解决方案

在3.0.1.Final之前的RestEasy版本



对于bean验证1.0,Resteasy具有使用hibernate的bean验证器实现的自定义验证提供程序。 p>

为了在Resteasy中获得验证并运行,您需要执行以下操作:


  1. resteasy-hibernatevalidator-provider 依赖关系添加到您的项目中。如果你使用maven,这里是maven pom条目:

     < dependency> 
    < groupId> org.jboss.resteasy< / groupId>
    < artifactId> resteasy-hibernatevalidator-provider< / artifactId>
    < version> $ {resteasy.version}< / version>
    < / dependency>


  2. 使用 @ValidateRequest 注释。

      @Named 
    @Path(/ users)
    @ValidateRequest
    public class UserResource extends BaseResource
    {
    @POST
    @Consumes({MediaType.APPLICATION_JSON})
    @Produces({MediaType.APPLICATION_JSON} )
    public Response createUser(@Valid User user)
    {
    // Do something Here Here
    }
    }
    / pre>

    Resteasy将自动检测类路径中的 HibernateValidatorAdapter ,并开始调用bean验证。

    / li>
  3. 创建一个 ExceptionMapper< MethodConstraintViolationException> 实现来处理验证错误。



    与Spring必须检查BindingResult不同,当在Resteasy中遇到验证错误时,hibernate验证器将抛出一个 MethodConstraintViolationException MethodConstraintViolationException 将包含其中的所有验证错误。

      @ Provider 
    public class MethodConstraintViolationExceptionMapper extends MyBaseExceptionMapper
    implements ExceptionMapper< MethodConstraintViolationException>
    {
    @Override
    public Response toResponse(MethodConstraintViolationException exception)
    {
    //在这里发生错误并创建响应。
    }
    }




RestEasy Version 3.0.1.Final



Resteasy的最新版本现在支持bean验证规范1.1,并更改了抛出的api和异常。


  1. 而不是 resteasy-hibernatevalidator-provider 你要
    需要 resteasy-validator-provider-11 依赖。


  2. 您不需要添加 @ValidateRequest 到您的资源类
    ,因为验证测试​​默认情况下打开
    resteasy-validator-provider-11


  3. 当检测到
    违例时,不要抛出 MethodConstraintViolationException RESTEasyViolationException
    将被抛出。


文件: 3.0.1.Fina l验证文档


Lets say I define a POJO with parameters that is passed to a REST call

 class MyVO {
    @NotNull
    @PathParam("name")
    private String name;

    @NotNull
    @PathParam("age")
    private Integer age;
    // getters and setters
 }

 public class RESTclass {
   public postData( @Form MyVO vo ) {
   }
 }

It automatically binds the objects in MyVO. But where do I get the validation errors? Does it trigger the validation during binding? If not, how to trigger the validations?

Spring does all these well. It has BindingResult parameter that you can inject. What is the equivalent here?

Any idea?

解决方案

RestEasy Versions Prior to 3.0.1.Final

For bean validation 1.0, Resteasy has a custom validation provider that uses hibernate's bean validator implementation under the covers.

In order to get validation up and running in Resteasy you need to do the following:

  1. Add the resteasy-hibernatevalidator-provider dependency to your project. Here is the maven pom entry if you are using maven:

    <dependency>
        <groupId>org.jboss.resteasy</groupId>
        <artifactId>resteasy-hibernatevalidator-provider</artifactId>
        <version>${resteasy.version}</version>
    </dependency>
    

  2. Annotate your resource classes where you want validation to occur with the @ValidateRequest annotation.

    @Named 
    @Path("/users") 
    @ValidateRequest 
    public class UserResource extends BaseResource 
    {   
        @POST
        @Consumes({MediaType.APPLICATION_JSON})
        @Produces({MediaType.APPLICATION_JSON})
        public Response createUser(@Valid User user)
        {
            //Do Something Here
        }
    }
    

    Resteasy will automatically detect the HibernateValidatorAdapter on the classpath and begin invoking bean validation.

  3. Create an ExceptionMapper<MethodConstraintViolationException> implementation to handle the validation errors.

    Unlike in Spring where you have to check the BindingResult, when validation errors are encountered in Resteasy the hibernate validator will throw a MethodConstraintViolationException. The MethodConstraintViolationException will contain all of the validation errors within it.

    @Provider
    public class MethodConstraintViolationExceptionMapper extends MyBaseExceptionMapper
            implements ExceptionMapper<MethodConstraintViolationException>
    {
        @Override
        public Response toResponse(MethodConstraintViolationException exception) 
        {
            //Do Something with the errors here and create a response.
        }
    }
    

RestEasy Version 3.0.1.Final

The latest version of Resteasy is now supporting bean validation spec 1.1 and has changed the api and exceptions thrown.

  1. Instead of the resteasy-hibernatevalidator-provider you are going to need the resteasy-validator-provider-11 dependency.

  2. You will not need to add @ValidateRequest to your resource classes as validation testing is turned on by default with resteasy-validator-provider-11.

  3. Instead of throwing a MethodConstraintViolationException when violations are detected, an instance of RESTEasyViolationException will be thrown.

Documentation: 3.0.1.Final Validation Documentation

这篇关于RestEasy - 参数绑定 - 验证和错误 - EJB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆