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

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

问题描述

可以说,我定义了一个POJO与传递给REST调用的参数

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 ) {
   }
 }

它会自动绑定在MyVO的对象。但是,在我得到验证错误?
是否绑定期间触发验证?如果不是这样,如何​​触发验证

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?

春季做所有这些很好。它有你可以注入BindingResult参数。
这里相当于是什么?

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

任何想法?

推荐答案

有关Bean验证1.0,RestEasy的有底层使用Hibernate的豆验证器实现自定义验证提供商。

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.

在为了得到验证并在运行RestEasy的,你需要做到以下几点:

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


  1. RestEasy的-hibernatevalidator提供商依赖添加到您的项目。这里是如果你正在使用maven Maven的POM条目:

  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>


  • 注释您希望与 @ValidateRequest 注释出现验证你的资源类。

  • 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的会自动检测 HibernateValidatorAdapter 在类路径上,并开始调用Bean验证。

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

    创建一个 ExceptionMapper&LT; MethodConstraintViolationException&GT; 实现来处理验证错误。

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

    不像在春天,你必须检查BindingResult,当RestEasy的遇到验证错误Hibernate验证将抛出一个 MethodConstraintViolationException 。在 MethodConstraintViolationException 将包含在其内的所有验证错误的。

    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的最新版本现在支持Bean验证规范1.1和改变抛出的API和异常。

    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. 相反的 RestEasy的-hibernatevalidator提供商你会
      需要在 RestEasy的验证器提供商-11 依赖。

    您不需要 @ValidateRequest 添加到您的资源类
    为验证测试​​默认打开与
    RestEasy的验证器提供商-11

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

    而不是抛出的 MethodConstraintViolationException
    在检测到违例, RESTEasyViolationException 的一个实例
    将被抛出。

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

    文档: 3.0.1.Final验证文件

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

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