1. Overview
1.概述
It’s easy to misuse bean validation constraints. For example, we might accidentally decorate a String attribute with the @Future constraint. Such mistakes can lead to unpredictable errors at runtime.
滥用bean validation约束是很容易的。例如,我们可能不小心用@Future约束来装饰一个String属性。这样的错误会在运行时导致不可预知的错误。
Fortunately, the Hibernate Validator Annotation Processor helps detect those issues at compile-time. Thanks to the errors it throws, we can catch these bugs earlier.
幸运的是,Hibernate Validator Annotation Processor有助于在编译时检测这些问题。由于它所抛出的错误,我们可以更早地发现这些错误。
In this tutorial, we’ll explore how to configure the processor, and we’ll look at some common problems it can find for us.
在本教程中,我们将探讨如何配置处理器,我们将看看它能为我们找到的一些常见问题。
2. Configuration
2.配置
2.1. Installation
2.1.安装
Let’s start by adding the annotation processor dependency to our pom.xml:
让我们先把注释处理器的依赖性添加到我们的 pom.xml 中。
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<compilerArgs>
<arg>-Averbose=true</arg>
<arg>-AmethodConstraintsSupported=true</arg>
<arg>-AdiagnosticKind=ERROR</arg>
</compilerArgs>
<annotationProcessorPaths>
<path>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator-annotation-processor</artifactId>
<version>6.2.0.Final</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
We should note that version 7 of this tool is only compatible with the jakarta.validation constraints:
我们应该注意,这个工具的第7版只与jakarta.validation约束兼容。
<dependency>
<groupId>jakarta.validation</groupId>
<artifactId>jakarta.validation-api</artifactId>
<version>3.0.1</version>
</dependency>
The processor also provides guidance on how to set it up for the major Java IDEs.
该处理器还提供了指导,说明如何为主要的Java IDE设置它。
2.2. Compiler Options
2.2.编译器选项</b
Let’s set up our processor compiler options:
让我们来设置我们的处理器编译器选项。
<compilerArgs>
<arg>-Averbose=true</arg>
<arg>-AmethodConstraintsSupported=true</arg>
<arg>-AdiagnosticKind=ERROR</arg>
</compilerArgs>
First, the diagnosticKind option targets the logging level. It’s preferable to keep the default ERROR value in order to catch problems at compile-time. All allowed values are referenced in the Diagnostic.Kind enum.
首先,diagnosticKind选项针对的是日志记录级别。最好是保持默认的ERROR值,以便在编译时发现问题。所有允许的值都在 Diagnostic.Kind枚举中引用。
Next, if we want to restrict annotation validation to getters only, we should set the methodConstraintsSupported option to false.
接下来,如果我们想把注解验证只限制在getters上,我们应该把methodConstraintsSupported选项设置为false。
Here, we’ve set verbose to true to get more output, but we can set it to false if we don’t want a lot of log output.
在这里,我们把verbose设置为true,以获得更多的输出,但如果我们不想要大量的日志输出,我们可以把它设置为false。
3. Common Constraint Problems
3.常见的限制性问题
The annotation processor comes with a set of predefined errors to check. Let’s have a closer look at three of them by using a simple Message class as an example:
注解处理器带有一组预定义的错误要检查。让我们以一个简单的Message类为例,来仔细看看其中的三个。
public class Message {
// constructor omitted
}
3.1. Only Getters Can Be Annotated
3.1.只有获取器可以被注释
First, this problem shouldn’t exist with the processor’s default options. As the name implies, it pops up when we annotate a non-getter method. We need to set the methodConstraintsSupported option to true to allow this.
首先,这个问题不应该存在于处理器的默认选项中。顾名思义,当我们注解一个非getter方法时,它就会弹出来。我们需要将methodConstraintsSupported选项设置为true以允许这样做。
Let’s add three annotated methods to our Message class:
让我们为我们的Message类添加三个注释方法。
@Min(3)
public boolean broadcast() {
return true;
}
@NotNull
public void archive() {
}
@AssertTrue
public boolean delete() {
return false;
}
Next, we set the methodConstraintsSupported option to false in our config:
接下来,我们在配置中把methodConstraintsSupported选项设置为false。
<compilerArgs>
<arg>AmethodConstraintsSupported=false</arg>
</compilerArgs>
Finally, the three methods will cause the processor to detect our problem:
最后,这三种方法将使处理器检测到我们的问题。
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] ${home}\baeldung\tutorials\javaxval\src\main\java\com\baeldung\javaxval\methodvalidation\model\ReservationManagement.java:[25,4] error: Constraint annotations must not be specified at methods, which are no valid JavaBeans getter methods.
[ERROR] ${home}\baeldung\tutorials\javaxval\src\main\java\com\baeldung\javaxval\hibernate\validator\ap\Message.java:[55,4] error: Constraint annotations must not be specified at methods, which are no valid JavaBeans getter methods.
[ERROR] ${home}\baeldung\tutorials\javaxval\src\main\java\com\baeldung\javaxval\hibernate\validator\ap\Message.java:[38,5] error: Constraint annotations must not be specified at methods, which are no valid JavaBeans getter methods.
[ERROR] ${home}\baeldung\tutorials\javaxval\src\main\java\com\baeldung\javaxval\hibernate\validator\ap\Message.java:[47,4] error: Constraint annotations must not be specified at methods, which are no valid JavaBeans getter methods.
[INFO] 4 errors
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 5.457 s
[INFO] Finished at: 2022-01-20T21:42:47Z
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.6.1:compile (default-compile) on project javaxval: Compilation failure: Compilation failure:
[ERROR] ${home}\baeldung\tutorials\javaxval\src\main\java\com\baeldung\javaxval\methodvalidation\model\ReservationManagement.java:[25,4] error: Constraint annotations must not be specified at methods, which are no valid JavaBeans getter methods.
[ERROR] ${home}\baeldung\tutorials\javaxval\src\main\java\com\baeldung\javaxval\hibernate\validator\ap\Message.java:[55,4] error: Constraint annotations must not be specified at methods, which are no valid JavaBeans getter methods.
[ERROR] ${home}\baeldung\tutorials\javaxval\src\main\java\com\baeldung\javaxval\hibernate\validator\ap\Message.java:[38,5] error: Constraint annotations must not be specified at methods, which are no valid JavaBeans getter methods.
[ERROR] ${home}\baeldung\tutorials\javaxval\src\main\java\com\baeldung\javaxval\hibernate\validator\ap\Message.java:[47,4] error: Constraint annotations must not be specified at methods, which are no valid JavaBeans getter methods.
It’s interesting to notice that the delete method is affected by the issue, even though technically, it’s properly annotated.
有趣的是,我们注意到delete方法受到了这个问题的影响,尽管从技术上讲,它被正确地注释了。。
For the next sections, we’ll set the methodConstraintsSupported option back to true.
在接下来的章节中,我们将把methodConstraintsSupported选项重新设置为true。
3.2. Only Non-Void Methods Can Be Annotated
3.2.只有非虚构的方法可以被注释
This problem states that we shouldn’t decorate void methods with constraint validations. We can see it in action by annotating an archive method in our Message class:
这个问题指出,我们不应该用约束验证来装饰void方法。我们可以通过注解Message类中的archive方法来看到它的作用。
@NotNull
public void archive() {
}
It causes the processor to raise an error:
它导致处理器引发一个错误。
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] ${home}\baeldung\tutorials\javaxval\src\main\java\com\baeldung\javaxval\hibernate\validator\ap\Message.java:[45,4] error: Void methods may not be annotated with constraint annotations.
[INFO] 1 error
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.078 s
[INFO] Finished at: 2022-01-20T21:35:08Z
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.6.1:compile (default-compile) on project javaxval: Compilation failure
[ERROR] ${home}\baeldung\tutorials\javaxval\src\main\java\com\baeldung\javaxval\hibernate\validator\ap\Message.java:[45,4] error: Void methods may not be annotated with constraint annotations.
3.3. Unsupported Type for an Annotation
3.3.不支持的注释类型
This final problem is the most common. It occurs when the annotation target data type doesn’t match the target property. To see it in action in our Message class, let’s add an incorrectly annotated String attribute to our Message class:
最后这个问题是最常见的。它发生在注释的目标数据类型与目标属性不匹配的时候。为了在我们的Message类中看到它的作用,让我们在我们的Message类中添加一个错误的注释String属性。
@Past
private String createdAt;
An error will occur due to the @Past annotation. In fact, only date types can use this constraint:
由于@Past注释,将发生错误。事实上,只有日期类型可以使用这个约束。
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] ${home}\baeldung\tutorials\javaxval\hibernate\validator\ap\Message.java:[20,5] error: The annotation @Past is disallowed for this data type.
[INFO] 1 error
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 4.892 s
[INFO] Finished at: 2022-01-20T21:29:15Z
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.6.1:compile (default-compile) on project javaxval: Compilation failure
[ERROR] ${home}\baeldung\tutorials\javaxval\src\main\java\com\baeldung\javaxval\hibernate\validator\ap\Message.java:[20,5] error: The annotation @Past is disallowed for this data type.
We’ll get a similar error if we apply the wrong annotation to a method having an unsupported return type:
如果我们将错误的注解应用于一个具有不支持的返回类型的方法,我们会得到一个类似的错误。
@Min(3)
public boolean broadcast() {
return true;
}
The processor error message is the same as the previous one:
处理器的错误信息与之前的相同。
[ERROR] COMPILATION ERROR :
[INFO] -------------------------------------------------------------
[ERROR] ${home}\baeldung\tutorials\javaxval\src\main\java\com\baeldung\javaxval\hibernate\validator\ap\Message.java:[37,5] error: The annotation @Min is disallowed for the return type of this method.
[INFO] 1 error
[INFO] -------------------------------------------------------------
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.761 s
[INFO] Finished at: 2022-01-20T21:38:28Z
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.6.1:compile (default-compile) on project javaxval: Compilation failure
[ERROR] ${home}\baeldung\tutorials\javaxval\src\main\java\com\baeldung\javaxval\hibernate\validator\ap\Message.java:[37,5] error: The annotation @Min is disallowed for the return type of this method.
4. Conclusion
4.结论
In this article, we tried the Hibernate Validator Annotation Processor.
在这篇文章中,我们尝试了Hibernate Validator注释处理器。
First, we installed it and configured its options. Then, we explored its behavior with three common constraint problems.
首先,我们安装了它并配置了它的选项。然后,我们探索了它在三个常见约束问题上的行为。
As always, the example code can be found over on GitHub.
一如既往,可以在GitHub上找到示例代码。