Last active
August 13, 2020 09:32
-
-
Save mloza/a2631f2f6383478431f05a1b257224a2 to your computer and use it in GitHub Desktop.
Kod źródłowy do wpisu o Java Bean Validation znajdującego się pod adresem: https://blog.mloza.pl/java-bean-validation-sprawdzanie-poprawnosci-przeslanych-danych/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Pet { | |
@NotBlank | |
private String name; | |
public Pet(@NotBlank String name) { | |
this.name = name; | |
} | |
public String getName() { | |
return name; | |
} | |
public Pet setName(String name) { | |
this.name = name; | |
return this; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<dependencies> | |
<dependency> | |
<groupId>javax.validation</groupId> | |
<artifactId>validation-api</artifactId> | |
<version>2.0.0.Final</version> | |
</dependency> | |
<dependency> | |
<groupId>org.hibernate.validator</groupId> | |
<artifactId>hibernate-validator</artifactId> | |
<version>6.1.5.Final</version> | |
</dependency> | |
<dependency> | |
<groupId>javax.el</groupId> | |
<artifactId>javax.el-api</artifactId> | |
<version>3.0.0</version> | |
</dependency> | |
<dependency> | |
<groupId>org.glassfish.web</groupId> | |
<artifactId>javax.el</artifactId> | |
<version>2.2.6</version> | |
</dependency> | |
</dependencies> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> | |
<groupId>org.example</groupId> | |
<artifactId>bean-validation</artifactId> | |
<version>1.0-SNAPSHOT</version> | |
<dependencies> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-web</artifactId> | |
<version>2.3.2.RELEASE</version> | |
</dependency> | |
<dependency> | |
<groupId>javax.validation</groupId> | |
<artifactId>validation-api</artifactId> | |
<version>2.0.0.Final</version> | |
</dependency> | |
<dependency> | |
<groupId>org.hibernate.validator</groupId> | |
<artifactId>hibernate-validator</artifactId> | |
<version>6.1.5.Final</version> | |
</dependency> | |
</dependencies> | |
<build> | |
<plugins> | |
<plugin> | |
<groupId>org.apache.maven.plugins</groupId> | |
<artifactId>maven-compiler-plugin</artifactId> | |
<version>3.8.1</version> | |
<configuration> | |
<release>14</release> | |
</configuration> | |
</plugin> | |
</plugins> | |
</build> | |
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ProgrammaticValidation { | |
public static void main(String[] args) { | |
ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); // #1 | |
Validator validator = factory.getValidator(); | |
User user = new User(); // #2 | |
user.setName(""); | |
user.setAge(10); | |
user.setAboutMe("short"); | |
user.setEmail("notAnEmail"); | |
user.setNotARobot(false); | |
Set<ConstraintViolation<User>> violations = validator.validate(user); // #3 | |
System.out.println(violations); | |
User validUser = new User(); // #4 | |
validUser.setName("Michal"); | |
validUser.setAge(30); | |
validUser.setAboutMe("A little bit longer about me text. Text length should be in size constraints"); | |
validUser.setEmail("valid@email.com"); | |
validUser.setNotARobot(true); | |
Set<ConstraintViolation<User>> noViolations = validator.validate(validUser); // #5 | |
System.out.println(noViolations); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class User { | |
@NotBlank | |
private String name; | |
@Min(value = 18, message = "You are to young to use our service") | |
@Max(value = 150, message = "People are not live that long") | |
private int age; | |
@AssertTrue | |
private boolean notARobot; | |
private String email; | |
@Size(min = 50, max = 255, message = "Create a text that is between 50 and 255 charters long") | |
private String aboutMe; | |
// getters, setters... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Valid | |
@NotNull | |
private Pet pet; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private Optional<@Pattern(regexp = "\\d{2}-\\d{3}") String> zipCode; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment