From d4d963a849f0678ef67f9d3c861126b7a65f3e8b Mon Sep 17 00:00:00 2001 From: Giuli1297 Date: Tue, 2 Nov 2021 10:12:36 -0300 Subject: [PATCH] Algunas validaciones --- curriculumsearch/pom.xml | 10 ++++++++++ curriculumsearch/src/main/java/com/roshka/controller/PostulanteController.java | 27 +++++++++++++++++++++++---- curriculumsearch/src/main/java/com/roshka/modelo/Postulante.java | 17 +++++++++++++++++ 3 files changed, 50 insertions(+), 4 deletions(-) diff --git a/curriculumsearch/pom.xml b/curriculumsearch/pom.xml index 6d8230f..b88363f 100644 --- a/curriculumsearch/pom.xml +++ b/curriculumsearch/pom.xml @@ -47,6 +47,16 @@ spring-boot-devtools true + + + org.springframework.boot + spring-boot-starter-validation + + + org.hibernate.validator + hibernate-validator + 6.0.13.Final + diff --git a/curriculumsearch/src/main/java/com/roshka/controller/PostulanteController.java b/curriculumsearch/src/main/java/com/roshka/controller/PostulanteController.java index a0d74fa..fa424bf 100644 --- a/curriculumsearch/src/main/java/com/roshka/controller/PostulanteController.java +++ b/curriculumsearch/src/main/java/com/roshka/controller/PostulanteController.java @@ -4,19 +4,22 @@ import java.text.SimpleDateFormat; import java.util.Date; import javax.servlet.http.HttpServletRequest; +import javax.validation.ConstraintViolationException; import com.roshka.modelo.Experiencia; import com.roshka.modelo.Postulante; import com.roshka.repositorio.PostulanteRepository; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.validation.FieldError; +import org.springframework.web.bind.MethodArgumentNotValidException; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.servlet.ModelAndView; @Controller - public class PostulanteController { @Autowired PostulanteRepository post; @@ -42,4 +45,20 @@ public class PostulanteController { return "redirect:/"; } + @ResponseStatus(HttpStatus.BAD_REQUEST) + @ExceptionHandler({MethodArgumentNotValidException.class}) + public ResponseEntity handleValidationExceptions( + MethodArgumentNotValidException ex) { + return ResponseEntity.status(HttpStatus.BAD_REQUEST) + .body(ex.getMessage()); + } + + @ResponseStatus(HttpStatus.BAD_REQUEST) + @ExceptionHandler({ConstraintViolationException.class}) + public ResponseEntity handleValidationExceptions2( + ConstraintViolationException ex) { + return ResponseEntity.status(HttpStatus.BAD_REQUEST) + .body(ex.getMessage()); + } + } diff --git a/curriculumsearch/src/main/java/com/roshka/modelo/Postulante.java b/curriculumsearch/src/main/java/com/roshka/modelo/Postulante.java index 80a0358..7cdfd4e 100644 --- a/curriculumsearch/src/main/java/com/roshka/modelo/Postulante.java +++ b/curriculumsearch/src/main/java/com/roshka/modelo/Postulante.java @@ -1,6 +1,7 @@ package com.roshka.modelo; import javax.persistence.*; +import javax.validation.constraints.*; import java.text.ParseException; import java.text.SimpleDateFormat; @@ -17,36 +18,52 @@ public class Postulante { private long id; @Column(name="nombre") + @NotBlank + @Size(max = 255) private String nombre; @Column(name = "apellido") + @NotBlank + @Size(max = 255) private String apellido; @Column(name = "ci") + @NotBlank + @Size(max = 120) private String ci; @Column(name = "correo") + @NotBlank + @Email(message = "Formato incorrecto de correo") private String correo; @Column(name = "ciudad") + @NotBlank + @Size(max = 120) private String ciudad; @Column(name = "telefono") + @NotBlank private String telefono; @Column(name = "fecha_nacimiento") + @NotNull + @Past private Date fechaNacimiento; @Column(name = "resumen") private String resumen; @Column(name = "nivel_ingles") + @Min(value = 1) + @Max(value = 5) private long nivelIngles; @Column(name = "curriculum") private String curriculum; @Column(name = "modalidad") + @NotNull private String modalidad; @Column(name = "disponibilidad") -- libgit2 0.26.0