BAEL-5123: Annotation Processor for Hibernate Validator (#11673)

* feat(wip): add Hibernate Validator AP example

* chore: change AP version to 6.2.0.Final

See https://hibernate.atlassian.net/browse/HV-1868

* revert(style): remove unnecessary formatter

See https://github.com/eugenp/tutorials/pull/11673#discussion_r784444016

* fix: remove unauthorised annotation

 The AP was triggering a compile time error because the annotation @NotBlank is disallowed for the Profile data type. That's why the CI was failing.

 This customer class is unrelated to the article being written.

* fix(style): revert indentation issue

See https://github.com/eugenp/tutorials/pull/11673/files#r788146422
This commit is contained in:
chrys exaucet 2022-02-05 21:28:05 +00:00 committed by GitHub
parent b97323ea90
commit 3f46e530e7
3 changed files with 128 additions and 3 deletions

View File

@ -15,7 +15,7 @@
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>${hibernate-validator.version}</version>
</dependency>
@ -36,10 +36,41 @@
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.version}</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
<fork>true</fork>
<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>${hibernate-validator.ap.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<hibernate-validator.version>6.0.13.Final</hibernate-validator.version>
<hibernate-validator.ap.version>6.2.0.Final</hibernate-validator.ap.version>
<maven.compiler.version>3.6.1</maven.compiler.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<javax.el.version>3.0.0</javax.el.version>
<org.springframework.version>5.0.2.RELEASE</org.springframework.version>
</properties>
</project>
</project>

View File

@ -20,7 +20,6 @@ public class Customer {
@PositiveOrZero
private OptionalInt numberOfOrders;
@NotBlank
private Profile profile;
public String getName() {

View File

@ -0,0 +1,95 @@
package com.baeldung.javaxval.hibernate.validator.ap;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Past;
import java.util.List;
import java.util.Optional;
public class Message {
@NotNull(message = "Content cannot be null")
private String content;
private boolean isDelivered;
private List<@NotBlank String> recipients;
// uncomment in order to trigger AP annotation detection
// The annotation @Past is disallowed for this data type.
// @Past
private String createdAt;
public String getContent() {
return content;
}
public Message(String content, boolean isDelivered, List<@NotBlank String> recipients, String createdAt) {
this.content = content;
this.isDelivered = isDelivered;
this.recipients = recipients;
this.createdAt = createdAt;
}
// uncomment in order to trigger AP annotation detection
// The annotation @Min is disallowed for the return type of this method.
// @Min(3)
public boolean broadcast() {
// setup a logic
// to send to recipients
return true;
}
// uncomment in order to trigger AP annotation detection
// Void methods may not be annotated with constraint annotations.
// @NotNull
public void archive() {
// archive the message
}
// uncomment in order to trigger AP annotation detection
// Constraint annotations must not be specified at methods, which are no valid JavaBeans getter methods.
// NOTE: add <arg>-AmethodConstraintsSupported=false</arg> to compiler args before
// @AssertTrue
public boolean delete() {
// delete the message
return false;
}
public void setContent(String content) {
this.content = content;
}
public boolean isDelivered() {
return isDelivered;
}
public void setDelivered(boolean delivered) {
isDelivered = delivered;
}
public List<String> getRecipients() {
return recipients;
}
public void setRecipients(List<String> recipients) {
this.recipients = recipients;
}
public void setCreatedAt(String createdAt) {
this.createdAt = createdAt;
}
public String getName() {
return content;
}
public void setName(String content) {
this.content = content;
}
public Optional<@Past String> getCreatedAt() {
return Optional.ofNullable(createdAt);
}
}