java validate的用法_java – @ValidateOnExecution什么时候应该和泽西一起使用?

我无法理解@ValidateOnExecution注释的本质.有人可以解释用例吗?

根据球衣的文档,资源方法的约束会自动验证.此代码段来自jersey’s example.

@GET

@NotNull

@HasId

public List getContacts() {

return StorageService.findByName("");

}

@GET

@Path("{id}")

@NotNull(message = "{contact.does.not.exist}")

@HasId

public ContactCard getContact(

@DecimalMin(value = "0", message = "{contact.wrong.id}")

@PathParam("id") final Long id) {

return StorageService.get(id);

}

如果约束在pojo中,您可以使用@Valid(See)触发验证.

@Path("/")

class MyResourceClass {

@POST

@Consumes("application/xml")

public void registerUser(@Valid User user) {

...

}

}

那么@ValidateOnExecution用于什么除了明确关闭验证?

解决方法:

根据Jersey latest documentation @ValidateOnExecution,注释应该用于下一个目的:

According to Bean Validation specification, validation is enabled by default only for the so called constrained methods. Getter methods as defined by the Java Beans specification are not constrained methods, so they will not be validated by default. The special annotation @ValidateOnExecution can be used to selectively enable and disable validation. For example, you can enable validation on method getEmail shown in Example

@Path("/")

class MyResourceClass {

@Email

@ValidateOnExecution

public String getEmail() {

return email;

}

...

}

The default value for the type attribute of @ValidateOnExecution is IMPLICIT which results in method getEmail being validated.

因此@ValidateOnExecution也可以用于至少启用getter-methods的验证.

标签:java,rest,jersey,validation

来源: https://codeday.me/bug/20190708/1400679.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值