简体   繁体   English

如何在DAO中同时使用Spring验证注释(Bean验证规范)

[英]How to use Spring Validation Annotation in DAO concurrently(Bean Validation Specification)

I have a User Entity and it's using Spring Validation Annotation[Edited - Bean Validation Specification] as follow. 我有一个用户实体,它使用的是Spring Validation Annotation [已编辑-Bean验证规范],如下所示。

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

This validation is ok when I am using for Registration Action, But I tried to use the same Entity(User) by different Action called Update and I want to void @NotEmpty Annotation. 当我将其用于注册操作时,此验证是可以的,但是我尝试通过名为Update的其他操作使用相同的Entity(User),并且我想使@NotEmpty注释无效。 How can I do that? 我怎样才能做到这一点?

Regards. 问候。

I have a User Entity and it's using Spring Validation Annotation as follow. 我有一个用户实体,它使用的是Spring验证注释,如下所示。

First of all, it's not "Spring Validation Annotation". 首先,它不是“ Spring Validation Annotation”。 These are annotations from the Bean Validation Specification (known as JSR-303 and JSR-349). 这些是Bean验证规范 (称为JSR-303和JSR-349)的注释。 Some annotations a non-standard and supplied by the validation provider (for example, Hibernate Validator). 某些注释是非标准的,并且由验证提供程序提供(例如,Hibernate Validator)。

But I tried to use the same Entity(User) by different Action called Update and I want to void @NotEmpty Annotation 但是我尝试通过名为Update的不同操作使用相同的Entity(User),并且我想使@NotEmpty注释无效

It could be achieved by using groups attribute of the annotation. 这可以通过使用批注的groups属性来实现。 In first scenario you will run all groups and in another scenario only some of them. 在第一种情况下,您将运行所有组,而在另一种情况下,将仅运行其中一些。 Unfortunately, the Specification (currently) doesn't support that (because @Valid doesn't allow to provide groups ). 不幸的是,规范(当前)不支持该规范(因为@Valid不允许提供groups )。

Here is where Spring's non-standard annotation @Validated comes to the rescue: you can specify validation groups with it! 这是Spring的非标准注释@Validated来源:您可以使用它指定验证组!

But it works only for validation models in the controller. 但是它仅适用于控制器中的验证模型。 It doesn't work as validation before persisting entity to the database because there is no way to specify groups ( Default group always used). 在将实体持久保存到数据库之前,它不能用作验证,因为无法指定组(始终使用Default组)。

As an example, you can use this question: Hibernate-validator Groups with Spring MVC 例如,您可以使用以下问题: 具有Spring MVC的Hibernate-validator组

You could remove the validation from the user entity in your domain model and move it to the service layer. 您可以从域模型中的用户实体中删除验证,然后将其移至服务层。 This makes more sense because the validation of the entity depends on the context. 这更有意义,因为实体的验证取决于上下文。

In your example, there are 2 use cases: 在您的示例中,有2个用例:

  • a user should be able to register 用户应该能够注册

  • a user should be able to change his data for example his password. 用户应该能够更改其数据,例如密码。

Then this can be modeled like in the code below. 然后,可以像下面的代码中那样对它进行建模。

 public class User {

        private final String email;
        private String password;

        public User(final String email, final String password) {

            this.email = email;
            this.password = password;
        }

        public void changePassword(final String password) {
            this.password = password;
        }

        public String getEmail() {
            return email;
        }

        public String getPassword() {
            return password;
        }

    }

This also has the advantage that your domain model is not dependent on an external library. 这还有一个优点,您的域模型不依赖于外部库。

You can move the validations from the domain layer to the service layer where the validation depends on the action that is performed. 您可以将验证从域层移动到服务层,其中验证取决于所执行的操作。 There is a registration command and an update command. 有一个注册命令和一个更新命令。

When a user registers, the email and the password should not be empty. 用户注册时,电子邮件和密码不应为空。

public class RegisterUserCommand {

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

The update command only requires that the email is not empty update命令仅要求电子邮件不为空

public class UpdateUserCommand {

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

In the service layer you can then do something like: 在服务层中,您可以执行以下操作:

public class UserService {

    private final UserDao userDao;

    public UserService(final UserDao userDao){
        this.userDao = userDao;
    }

    public User register(final RegisterUserCommand command) {
        User user = new User(command.getEmail(), command.getPassword());
        return userDao.save(user);
    }

    public User update(final UpdateUserCommand command) {
        User user = userDao.findUserByEmail(command.getEmail());
        user.changePassword(command.getPassword);
        return user;

    }

}

This is of course only an example because I don't know how the rest of your code is implemented. 当然,这只是一个例子,因为我不知道其余代码是如何实现的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM