Resteasy Bean验证未被调用 [英] Resteasy Bean Validation Not Being Invoked
问题描述
我有一个Resteasy服务,它通过Resteasy的 SpringContextLoaderListener
使用Spring。这是基于Resteasy版本 3.0-beta-6
。
I have a Resteasy service that uses Spring through Resteasy's SpringContextLoaderListener
. This is built on Resteasy version 3.0-beta-6
.
我想在传入时使用bean验证请求,但我无法让Resteasy调用验证器。它的作用就像没有配置验证,只是将方法传递给无效的输入对象。
I would like to use bean validation on the incoming requests, but I can not get Resteasy to call the validator. It acts like there is no validation configured and simply passes the method the invalid input object.
- 如何在Resteasy中启用bean验证?
我尝试过什么
我已完成以下操作:
What I've Tried
I have done the following:
- 使用
@ValidateRequest <注释我的资源类
- 使用
@Valid
注释方法参数 - 注释对我的输入类的约束。
- 在
resteasy-hibernatevalidator-provider
- Annotated my resource class with
@ValidateRequest
- Annotated the method parameter with
@Valid
- Annotated the constraints on my input class.
- Added a dependency on
resteasy-hibernatevalidator-provider
资源:
Resource:
@Named
@Path("users")
@ValidateRequest
public class UserResource
{
/**
*
* @param user
*
* curl -x POST http://localhost:7016/api/1.0/users
*
*/
@POST
@Consumes({MediaType.APPLICATION_JSON})
@Produces({MediaType.APPLICATION_JSON})
public Response createUser(@Valid User user)
{
//User creation logic here.
}
}
用户POJO:
User POJO:
@JsonPropertyOrder({
"user_id",
"user_name",
"email"
})
public class User
{
@JsonProperty("user_id")
private Long userId;
@JsonProperty("user_name")
@NotNull(message = "Username must be provided")
private String username;
@Email(message = "Invalid email address.")
private String email;
//Getters and Setters Removed for Brevity
}
用于验证的POM条目:
POM Entry for Validation:
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-hibernatevalidator-provider</artifactId>
<version>${resteasy.version}</version>
</dependency>
resteasy-hibernatevalidator-provider
依赖带来在 HibernateValidatorContextResolver
及其关联的 HibernateValidatorAdapter
。
The resteasy-hibernatevalidator-provider
dependency brings in the HibernateValidatorContextResolver
and its associated HibernateValidatorAdapter
.
我将我的pom中的Resteasy版本恢复为 2.3 .5.Final
并且bean验证在没有任何代码更改的情况下开始工作。
I reverted the Resteasy version in my pom to 2.3.5.Final
and bean validation started working without any code changes.
推荐答案
使用Resteasy运行' 3.0.6.Final'和Spring'4.1.0.RELEASE'。
Running with Resteasy '3.0.6.Final' and Spring '4.1.0.RELEASE'.
'resteasy-hibernatevalidator-provider'不评估@Valid带注释的参数。使用'resteasy-validator-provider-11'可以使一切正常运行,并且使用Hiberbate验证器'5.0.1.Final'而不是在使用'resteasy-hibernatevalidator-provider'时需要Hibernate验证器版本4。
The 'resteasy-hibernatevalidator-provider' does not evaluate the @Valid annotated params. Using the 'resteasy-validator-provider-11' makes everything work and as a bonus is using Hiberbate validator '5.0.1.Final' instead of needing a Hibernate validator version 4 when using the 'resteasy-hibernatevalidator-provider'.
这篇关于Resteasy Bean验证未被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!