Add validation module: Bean Validation on Spring Boot 4.2.0 with Hibernate Validator 9.1.6, built-in and custom constraints, groups, method validation, ProblemDetail errors, message i18n, JPA integration and tests, with real transcripts
Co-Authored-By: Claude Sonnet 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_017ic4b4RzcCS5JCAgcxLz7T
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.ankurm</groupId>
|
||||
<artifactId>validation-plain-java</artifactId>
|
||||
<version>1.0.0</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.release>25</maven.compiler.release>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<!-- The engine. It brings jakarta.validation-api with it. -->
|
||||
<dependency>
|
||||
<groupId>org.hibernate.validator</groupId>
|
||||
<artifactId>hibernate-validator</artifactId>
|
||||
<version>9.1.6.Final</version>
|
||||
</dependency>
|
||||
<!-- A Jakarta Expression Language implementation, needed by the default message interpolator. -->
|
||||
<dependency>
|
||||
<groupId>org.glassfish.expressly</groupId>
|
||||
<artifactId>expressly</artifactId>
|
||||
<version>6.0.0</version>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,25 @@
|
||||
import jakarta.validation.Validation;
|
||||
import jakarta.validation.Validator;
|
||||
import jakarta.validation.ValidatorFactory;
|
||||
import jakarta.validation.constraints.Size;
|
||||
import org.hibernate.validator.messageinterpolation.ParameterMessageInterpolator;
|
||||
|
||||
/** Hibernate Validator with no framework. Run with the argument "no-el" to skip the EL requirement. */
|
||||
public class PlainJava {
|
||||
|
||||
record User(@Size(min = 3, message = "need {min} characters, got ${validatedValue}") String name) { }
|
||||
|
||||
public static void main(String[] args) {
|
||||
var config = Validation.byDefaultProvider().configure();
|
||||
if (args.length > 0) {
|
||||
config.messageInterpolator(new ParameterMessageInterpolator());
|
||||
}
|
||||
// Building a factory is expensive: build one per application, close it on shutdown.
|
||||
// The Validator it hands out is thread-safe.
|
||||
try (ValidatorFactory factory = config.buildValidatorFactory()) {
|
||||
Validator validator = factory.getValidator();
|
||||
validator.validate(new User("ab")).forEach(v ->
|
||||
System.out.println(v.getPropertyPath() + ": " + v.getMessage()));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
import java.util.Arrays;
|
||||
|
||||
/** Where does an annotation end up when its @Target omits RECORD_COMPONENT? */
|
||||
public class RecordTarget {
|
||||
|
||||
@Target(ElementType.FIELD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@interface FieldOnly { }
|
||||
|
||||
record Message(@FieldOnly String text) { }
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
System.out.println("compiled: @Target(FIELD) annotation on a record component");
|
||||
System.out.println("on the record component: "
|
||||
+ Arrays.toString(Message.class.getRecordComponents()[0].getAnnotations()));
|
||||
System.out.println("on the private field: "
|
||||
+ Arrays.toString(Message.class.getDeclaredField("text").getAnnotations()));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user