如何避免在 hibernate-validator 期间在每个内部类字段上添加 @Valid?

发布于 2025-01-15 00:55:58 字数 1143 浏览 2 评论 0原文

我目前正在开发一个应用程序,在内部类上添加一些验证,例如 @NotNull、@Min、@Max 等。

为了使验证工作,我需要添加 @Valid 在每个使用内部类的字段上。有没有办法避免在每个对象上添加@Valid,而是在类上添加一些注释,以便它可以适用于该类中的所有字段?

我目前正在使用以下库来实现验证:

<dependency>
    <groupId>io.quarkus</groupId>
    <artifactId>quarkus-hibernate-validator</artifactId>
</dependency>

我尝试在类上添加 @Validated 但似乎此注释在此 Maven 依赖项中不可用。有人可以让我知道我需要改变什么吗?

以下是一个有效的简单示例,但我想删除必须在每个字段上添加的 @Valid 。如果我不添加 @Valid 那么这些内部类将不会被验证。

public class Book {
    @NotNull(message="Book ID cannot be NULL")
    private int bookId;

    @Valid
    private List<Author> author;

    @Valid
    private List<Publication> author;
}


public class Author {
    @NotNull(message="Author ID cannot be NULL")
    private int authorID;

    @NotNull(message="Author Name cannot be NULL")
    private String name;
}


public class Publication {
    @NotNull(message="Publication ID cannot be NULL")
    private int authorID;

    @NotNull(message="Publication Name cannot be NULL")
    private String name;
}

I am currently developing an application within that I am adding a few validations on an inner class such as @NotNull, @Min, @Max, etc.

To make the validations work I need to add the @Valid on each and every field which is making use of the inner class. Is there a way to avoid adding the @Valid on each and every object rather add some annotations on the Class so it can be applicable to all the fields within that class?

I am currently using the following library to achieve the validations:

<dependency>
    <groupId>io.quarkus</groupId>
    <artifactId>quarkus-hibernate-validator</artifactId>
</dependency>

I tried to add the @Validated on the class but seems like this annotation is not available in this maven dependency. Can someone please let me know what I need to change?

Following is a simple example that is working but I would like to remove @Valid that I have to add on each field. If I do not add @Valid then those inner classes won't be validated.

public class Book {
    @NotNull(message="Book ID cannot be NULL")
    private int bookId;

    @Valid
    private List<Author> author;

    @Valid
    private List<Publication> author;
}


public class Author {
    @NotNull(message="Author ID cannot be NULL")
    private int authorID;

    @NotNull(message="Author Name cannot be NULL")
    private String name;
}


public class Publication {
    @NotNull(message="Publication ID cannot be NULL")
    private int authorID;

    @NotNull(message="Publication Name cannot be NULL")
    private String name;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

峩卟喜欢 2025-01-22 00:55:58

没有办法做你想做的事,除非你编写自己的 Quarkus 扩展来在构建时添加注释。

不过,这将是一些相当复杂的工作,因为您需要添加一些字节码转换以在您想要的位置添加注释。

另外,您应该在 List 内添加 @Valid 注释,例如 List<@Valid Publication>,而不是在字段级别。这样就更优化了。

There is no way to do what you want to do, except if you write your own Quarkus extension that will add the annotations at build time.

It will be some rather involved work, though, as you will need to add some bytecode transformation to add the annotations where you want them.

Also, you should add the @Valid annotations inside the List e.g. List<@Valid Publication> rather than at the field level. It's more optimized this way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文