java - 如何在DAO中并发使用Spring Validation Annotation(Bean Validation Specification)

标签 java spring spring-boot bean-validation

我有一个用户实体,它正在使用 Spring 验证注解 [已编辑 - Bean 验证规范],如下所示。

@NotEmpty(message = "{warn.null.user.password}")
private String            password;

当我用于注册操作时,此验证是可以的,但我试图通过称为更新的不同操作使用相同的实体(用户),我想取消 @NotEmpty 注释。我该怎么做?

问候。

最佳答案

I have a User Entity and it's using Spring Validation Annotation as follow.

首先,它不是“Spring Validation Annotation”。这些是来自 Bean Validation Specification 的注释(称为 JSR-303 和 JSR-349)。一些非标准注释由验证提供程序(例如,Hibernate Validator)提供。

But I tried to use the same Entity(User) by different Action called Update and I want to void @NotEmpty Annotation

这可以通过使用注释的 groups 属性来实现。在第一种情况下,您将运行所有组,而在另一种情况下,您将只运行其中的一些组。不幸的是,规范(当前)不支持(因为 @Valid 不允许提供 groups)。

这里是Spring的非标准注解@Validated的地方来拯救:你可以用它指定验证组!

但它仅适用于 Controller 中的验证模型。在将实体保存到数据库之前,它不能用作验证,因为无法指定组(始终使用 Default 组)。

例如,您可以使用这个问题:Hibernate-validator Groups with Spring MVC

关于java - 如何在DAO中并发使用Spring Validation Annotation(Bean Validation Specification),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41549180/

相关文章:

javax.el.PropertyNotFoundException :/demo. xhtml @24,55 值 ="#{UserBean.favYear3}": Target Unreachable, 标识符 'UserBean' 解析为 null

java - Spring AOP启动时间慢

spring - 在 SpringDataRest 中,如何在收到 http 请求时添加自定义逻辑

spring - 如何在 IntelliJ 上使用 @ConfigurationProperties 配置 Spring Boot 配置注释处理器?

java - 连接到运行在不同 pod 上的 gRPC 服务器

spring - 如何在Spring Boot应用中为@Configuration注解的类编写单元测试用例

java - 未绑定(bind) play.db.Database 的实现

java - KdTree 最近邻搜索算法无法正常工作

java - springboot缓存自动刷新redis和apache ignite

java - spring boot 的 netty 上下文路径