JAVA-20499 | Merging javax-validation-advanced module in javaxval-2. (#14362)

This commit is contained in:
Gaetano Piazzolla 2023-07-05 19:23:06 +02:00 committed by GitHub
parent fae290a102
commit 6130c8436d
14 changed files with 162 additions and 202 deletions

View File

@ -1,6 +0,0 @@
.classpath
.project
.settings/
target/
bin/

View File

@ -1,6 +0,0 @@
## Java Bean Validation Examples
This module contains articles about Bean Validation.
### Relevant Articles:
- [Object Validation After Deserialization](https://www.baeldung.com/java-object-validation-deserialization)

View File

@ -1,33 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>javax-validation-advanced</artifactId>
<name>javax-validation-advanced</name>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
<version>${spring.boot.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.databind.version}</version>
</dependency>
</dependencies>
<properties>
<spring.boot.version>2.7.5</spring.boot.version>
<jackson.databind.version>2.14.0</jackson.databind.version>
</properties>
</project>

View File

@ -6,4 +6,5 @@ This module contains articles about Bean Validation.
- [Method Constraints with Bean Validation 2.0](https://www.baeldung.com/javax-validation-method-constraints) - [Method Constraints with Bean Validation 2.0](https://www.baeldung.com/javax-validation-method-constraints)
- [Guide to ParameterMessageInterpolator](https://www.baeldung.com/hibernate-parametermessageinterpolator) - [Guide to ParameterMessageInterpolator](https://www.baeldung.com/hibernate-parametermessageinterpolator)
- [Hibernate Validator Annotation Processor in Depth](https://www.baeldung.com/hibernate-validator-annotation-processor) - [Hibernate Validator Annotation Processor in Depth](https://www.baeldung.com/hibernate-validator-annotation-processor)
- [Object Validation After Deserialization](https://www.baeldung.com/java-object-validation-deserialization)
- More articles: [[<-- prev]](../javaxval) - More articles: [[<-- prev]](../javaxval)

View File

@ -25,6 +25,11 @@
<version>${spring.boot.version}</version> <version>${spring.boot.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.databind.version}</version>
</dependency>
</dependencies> </dependencies>
<!-- uncomment in order to enable Hibernate Validator Anotation Processor --> <!-- uncomment in order to enable Hibernate Validator Anotation Processor -->
@ -39,6 +44,7 @@
<properties> <properties>
<spring.boot.version>3.0.4</spring.boot.version> <spring.boot.version>3.0.4</spring.boot.version>
<jackson.databind.version>2.14.0</jackson.databind.version>
</properties> </properties>
</project> </project>

View File

@ -1,20 +1,20 @@
package com.baeldung.javaxval.afterdeserialization; package com.baeldung.javaxval.afterdeserialization;
import com.fasterxml.jackson.databind.BeanDescription; import com.fasterxml.jackson.databind.BeanDescription;
import com.fasterxml.jackson.databind.DeserializationConfig; import com.fasterxml.jackson.databind.DeserializationConfig;
import com.fasterxml.jackson.databind.JsonDeserializer; import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.deser.BeanDeserializer; import com.fasterxml.jackson.databind.deser.BeanDeserializer;
import com.fasterxml.jackson.databind.deser.BeanDeserializerModifier; import com.fasterxml.jackson.databind.deser.BeanDeserializerModifier;
public class BeanDeserializerModifierWithValidation extends BeanDeserializerModifier { public class BeanDeserializerModifierWithValidation extends BeanDeserializerModifier {
@Override @Override
public JsonDeserializer<?> modifyDeserializer(DeserializationConfig config, BeanDescription beanDesc, JsonDeserializer<?> deserializer) { public JsonDeserializer<?> modifyDeserializer(DeserializationConfig config, BeanDescription beanDesc, JsonDeserializer<?> deserializer) {
if (deserializer instanceof BeanDeserializer) { if (deserializer instanceof BeanDeserializer) {
return new BeanDeserializerWithValidation((BeanDeserializer) deserializer); return new BeanDeserializerWithValidation((BeanDeserializer) deserializer);
} }
return deserializer; return deserializer;
} }
} }

View File

@ -1,40 +1,40 @@
package com.baeldung.javaxval.afterdeserialization; package com.baeldung.javaxval.afterdeserialization;
import java.io.IOException; import java.io.IOException;
import java.util.Set; import java.util.Set;
import javax.validation.ConstraintViolation; import jakarta.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException; import jakarta.validation.ConstraintViolationException;
import javax.validation.Validation; import jakarta.validation.Validation;
import javax.validation.Validator; import jakarta.validation.Validator;
import javax.validation.ValidatorFactory; import jakarta.validation.ValidatorFactory;
import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.BeanDeserializer; import com.fasterxml.jackson.databind.deser.BeanDeserializer;
import com.fasterxml.jackson.databind.deser.BeanDeserializerBase; import com.fasterxml.jackson.databind.deser.BeanDeserializerBase;
public class BeanDeserializerWithValidation extends BeanDeserializer { public class BeanDeserializerWithValidation extends BeanDeserializer {
private static final ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); private static final ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
private static final Validator validator = factory.getValidator(); private static final Validator validator = factory.getValidator();
protected BeanDeserializerWithValidation(BeanDeserializerBase src) { protected BeanDeserializerWithValidation(BeanDeserializerBase src) {
super(src); super(src);
} }
@Override @Override
public Object deserialize(JsonParser p, DeserializationContext ctxt) throws IOException { public Object deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
Object instance = super.deserialize(p, ctxt); Object instance = super.deserialize(p, ctxt);
validate(instance); validate(instance);
return instance; return instance;
} }
public <T> void validate(T t) { public <T> void validate(T t) {
Set<ConstraintViolation<T>> violations = validator.validate(t); Set<ConstraintViolation<T>> violations = validator.validate(t);
if (!violations.isEmpty()) { if (!violations.isEmpty()) {
throw new ConstraintViolationException(violations); throw new ConstraintViolationException(violations);
} }
} }
} }

View File

@ -1,14 +1,14 @@
package com.baeldung.javaxval.afterdeserialization; package com.baeldung.javaxval.afterdeserialization;
import javax.validation.constraints.Size; import jakarta.validation.constraints.Size;
public class Student { public class Student {
@Size(min = 5, max = 10, message = "Student's name must be between 5 and 10 characters") @Size(min = 5, max = 10, message = "Student's name must be between 5 and 10 characters")
private String name; private String name;
public String getName() { public String getName() {
return name; return name;
} }
} }

View File

@ -1,24 +1,24 @@
package com.baeldung.javaxval.afterdeserialization; package com.baeldung.javaxval.afterdeserialization;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule; import com.fasterxml.jackson.databind.module.SimpleModule;
public class StudentDeserializerWithValidation { public class StudentDeserializerWithValidation {
public static Student readStudent(InputStream inputStream) throws IOException { public static Student readStudent(InputStream inputStream) throws IOException {
ObjectMapper mapper = getObjectMapperWithValidation(); ObjectMapper mapper = getObjectMapperWithValidation();
return mapper.readValue(inputStream, Student.class); return mapper.readValue(inputStream, Student.class);
} }
private static ObjectMapper getObjectMapperWithValidation() { private static ObjectMapper getObjectMapperWithValidation() {
SimpleModule validationModule = new SimpleModule(); SimpleModule validationModule = new SimpleModule();
validationModule.setDeserializerModifier(new BeanDeserializerModifierWithValidation()); validationModule.setDeserializerModifier(new BeanDeserializerModifierWithValidation());
ObjectMapper mapper = new ObjectMapper(); ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(validationModule); mapper.registerModule(validationModule);
return mapper; return mapper;
} }
} }

View File

@ -1,51 +1,51 @@
package com.baeldung.javaxval; package com.baeldung.javaxval;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import javax.validation.ConstraintViolationException; import jakarta.validation.ConstraintViolationException;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import com.baeldung.javaxval.afterdeserialization.Student; import com.baeldung.javaxval.afterdeserialization.Student;
import com.baeldung.javaxval.afterdeserialization.StudentDeserializerWithValidation; import com.baeldung.javaxval.afterdeserialization.StudentDeserializerWithValidation;
public class StudentDeserializerWithValidationUnitTest { public class StudentDeserializerWithValidationUnitTest {
private final String EXPECTED_ERROR_MESSAGE = "name: Student's name must be between 5 and 10 characters"; private final String EXPECTED_ERROR_MESSAGE = "name: Student's name must be between 5 and 10 characters";
private final String EXPECTED_STUDENT_NAME = "Daniel"; private final String EXPECTED_STUDENT_NAME = "Daniel";
private final String NAME_TOO_LONG_STUDENT_FILE = "nameTooLongStudent.json"; private final String NAME_TOO_LONG_STUDENT_FILE = "nameTooLongStudent.json";
private final String NAME_TOO_SHORT_STUDENT_FILE = "nameTooShortStudent.json"; private final String NAME_TOO_SHORT_STUDENT_FILE = "nameTooShortStudent.json";
private final String SUBDIRECTORY = "afterdeserialization/"; private final String SUBDIRECTORY = "afterdeserialization/";
private final String VALID_STUDENT_FILE = "validStudent.json"; private final String VALID_STUDENT_FILE = "validStudent.json";
@Test @Test
void givenValidStudent_WhenReadStudent_ThenReturnStudent() throws IOException { void givenValidStudent_WhenReadStudent_ThenReturnStudent() throws IOException {
InputStream inputStream = getInputStream(VALID_STUDENT_FILE); InputStream inputStream = getInputStream(VALID_STUDENT_FILE);
Student result = StudentDeserializerWithValidation.readStudent(inputStream); Student result = StudentDeserializerWithValidation.readStudent(inputStream);
assertEquals(EXPECTED_STUDENT_NAME, result.getName()); assertEquals(EXPECTED_STUDENT_NAME, result.getName());
} }
@Test @Test
void givenStudentWithTooShortName_WhenReadStudent_ThenThrows() { void givenStudentWithTooShortName_WhenReadStudent_ThenThrows() {
InputStream inputStream = getInputStream(NAME_TOO_SHORT_STUDENT_FILE); InputStream inputStream = getInputStream(NAME_TOO_SHORT_STUDENT_FILE);
ConstraintViolationException constraintViolationException = assertThrows(ConstraintViolationException.class, () -> StudentDeserializerWithValidation.readStudent(inputStream)); ConstraintViolationException constraintViolationException = assertThrows(ConstraintViolationException.class, () -> StudentDeserializerWithValidation.readStudent(inputStream));
assertEquals(EXPECTED_ERROR_MESSAGE, constraintViolationException.getMessage()); assertEquals(EXPECTED_ERROR_MESSAGE, constraintViolationException.getMessage());
} }
@Test @Test
void givenStudentWithTooLongName_WhenReadStudent_ThenThrows() { void givenStudentWithTooLongName_WhenReadStudent_ThenThrows() {
InputStream inputStream = getInputStream(NAME_TOO_LONG_STUDENT_FILE); InputStream inputStream = getInputStream(NAME_TOO_LONG_STUDENT_FILE);
ConstraintViolationException constraintViolationException = assertThrows(ConstraintViolationException.class, () -> StudentDeserializerWithValidation.readStudent(inputStream)); ConstraintViolationException constraintViolationException = assertThrows(ConstraintViolationException.class, () -> StudentDeserializerWithValidation.readStudent(inputStream));
assertEquals(EXPECTED_ERROR_MESSAGE, constraintViolationException.getMessage()); assertEquals(EXPECTED_ERROR_MESSAGE, constraintViolationException.getMessage());
} }
private InputStream getInputStream(String fileName) { private InputStream getInputStream(String fileName) {
return getClass().getClassLoader() return getClass().getClassLoader()
.getResourceAsStream(SUBDIRECTORY + fileName); .getResourceAsStream(SUBDIRECTORY + fileName);
} }
} }

View File

@ -850,7 +850,6 @@
<module>javax-sound</module> <module>javax-sound</module>
<module>javaxval</module> <module>javaxval</module>
<module>javaxval-2</module> <module>javaxval-2</module>
<module>javax-validation-advanced</module>
<module>jetbrains</module> <module>jetbrains</module>
<module>jgit</module> <module>jgit</module>
<module>jib</module> <module>jib</module>
@ -1120,7 +1119,6 @@
<module>javax-sound</module> <module>javax-sound</module>
<module>javaxval</module> <module>javaxval</module>
<module>javaxval-2</module> <module>javaxval-2</module>
<module>javax-validation-advanced</module>
<module>jetbrains</module> <module>jetbrains</module>
<module>jgit</module> <module>jgit</module>
<module>jib</module> <module>jib</module>