RestEasy - 参数绑定 - 验证和错误 - EJB [英] RestEasy - Parameters Binding - Validation and Errors - EJB
问题描述
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中获得验证并运行,您需要执行以下操作:
-
将
resteasy-hibernatevalidator-provider
依赖关系添加到您的项目中。如果你使用maven,这里是maven pom条目:< dependency>
< groupId> org.jboss.resteasy< / groupId>
< artifactId> resteasy-hibernatevalidator-provider< / artifactId>
< version> $ {resteasy.version}< / version>
< / dependency>
-
使用
@ValidateRequest
注释。@Named
/ pre>
@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
}
}
Resteasy将自动检测类路径中的
/ li>HibernateValidatorAdapter
,并开始调用bean验证。
-
创建一个
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和异常。
-
而不是
resteasy-hibernatevalidator-provider
你要
需要resteasy-validator-provider-11
依赖。 -
您不需要添加
@ValidateRequest
到您的资源类
,因为验证测试默认情况下打开
resteasy-validator-provider-11
。 -
当检测到
违例时,不要抛出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:
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>
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.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
. TheMethodConstraintViolationException
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.
Instead of the
resteasy-hibernatevalidator-provider
you are going to need theresteasy-validator-provider-11
dependency.You will not need to add
@ValidateRequest
to your resource classes as validation testing is turned on by default withresteasy-validator-provider-11
.Instead of throwing a
MethodConstraintViolationException
when violations are detected, an instance ofRESTEasyViolationException
will be thrown.
Documentation: 3.0.1.Final Validation Documentation
这篇关于RestEasy - 参数绑定 - 验证和错误 - EJB的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!