Skip to content

Instantly share code, notes, and snippets.

@mloza

mloza/Pet.java

Last active Aug 13, 2020
Embed
What would you like to do?
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/
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;
}
}
<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>
<?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>
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);
}
}
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;
@Email
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...
}
@Valid
@NotNull
private Pet pet;
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
You can’t perform that action at this time.