https://github.com/jwcarman/techolympics
https://github.com/jwcarman/techolympics
Last synced: 4 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/jwcarman/techolympics
- Owner: jwcarman
- Created: 2019-02-14T04:22:44.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2019-02-23T06:00:30.000Z (over 6 years ago)
- Last Synced: 2025-01-13T03:13:41.711Z (5 months ago)
- Language: Java
- Size: 58.6 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Validating Data With the Bean Validation API (JSR-380)
### Validate Registrations
1. Add a ```@Validated``` annotation to the ```RegistrationService``` interface.
2. Add ```@NotNull``` and ```@Valid``` annotations to the ```Registration``` parameter of the ```registerStudent()``` method in the ```RegistrationService``` interface.
3. Add an ```@Email``` annotation to the ```email``` field in the ```Registration``` class.
4. Add ```@NotEmpty``` constraints to the rest of the fields in the ```Registration``` class.5. In your browser, try to register a student with a invalid data.
- What happens?
### Validate Email Parameters
1. Add an ```@Email``` annotation to the ```email``` parameter of the ```getStudentRegistration()``` and ```unregisterStudent()``` methods in the ```RegistrationService``` interface.
2. In your browser, try to pass an invalid email address to either of these methods.
- What happens?
### Handle ConstraintViolationExceptions
1. Add the following method to the ```ErrorHandlerAdvice``` class:
```java
@ExceptionHandler(ConstraintViolationException.class)
@ResponseStatus(HttpStatus.BAD_REQUEST)
public List onConstraintViolation(ConstraintViolationException e) {
return e.getConstraintViolations().stream()
.map(ConstraintViolation::getMessage)
.collect(Collectors.toList());
}
```2. In your browser, perform an operation that violates a validation constraint.
- What happens now?
### Custom Error Messages1. Modify the ```@Email``` constraint on the ```email``` field in the ```Registration``` class:
```java
@Email(message = "{registration.email.invalid}")
```
2. Add the following line to the ```ValidationMessages.properties``` file:```properties
registration.email.invalid=Registration email value "${validatedValue}" is invalid.
```3. Try to register a student with an invalid email address.
- Do you see your custom message?
4. **BONUS**: figure out how to use the other error messages in the ```ValidationMessages.properties``` file.