Spring Boot

1.0 Annotation

1.1 Java bean validations


# Entity

@NotNull
@Size
@Min 
@Max 
@Email 
@NotEmpty 
@NotBlank

# POM

<dependency> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-validation</artifactId> 
</dependency>


# Exception Handler

    import java.util.HashMap;
    import java.util.Map;
    
    import org.springframework.http.HttpHeaders;
    
    import org.springframework.http.HttpStatus;
    import org.springframework.http.ResponseEntity;
    import org.springframework.validation.FieldError;
    import org.springframework.web.bind.MethodArgumentNotValidException;
    import org.springframework.web.bind.annotation.ControllerAdvice;
    import org.springframework.web.context.request.WebRequest;
    import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
    
    @ControllerAdvice
    public class ValidationHandler extends ResponseEntityExceptionHandler{
        
        @Override
        protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex,
                HttpHeaders headers, HttpStatus status, WebRequest request) {
            
            Map<String, String> errors = new HashMap<>();
            ex.getBindingResult().getAllErrors().forEach((error) ->{
                
                String fieldName = ((FieldError) error).getField();
                String message = error.getDefaultMessage();
                errors.put(fieldName, message);
            });
            return new ResponseEntity<Object>(errors, HttpStatus.BAD_REQUEST);
        }
    }    

2.1 Dispatcher Servlet?

3.1 Goals

3.1.1 Spring Boot Starter

3.1.2 Spring Boot Auto Configurations