From 6064d543505275380c7068f9c13d6cf8b22a485b Mon Sep 17 00:00:00 2001 From: Pezcue Date: Sun, 14 Apr 2024 16:24:46 -0400 Subject: [PATCH] Swagger and Exception Handler --- pom.xml | 12 ++++++- .../java/com/example/rest/DTO/ProjectDTO.java | 2 +- .../example/rest/Exceptions/ApiException.java | 35 +++++++++++++++++++ .../rest/Exceptions/ApiExceptionHandler.java | 20 +++++++++++ .../rest/Exceptions/ApiRequestException.java | 11 ++++++ .../rest/Services/ProjectServiceImpl.java | 7 ++-- .../rest/Services/TaskServiceImpl.java | 7 ++-- src/main/resources/application.properties | 5 ++- 8 files changed, 90 insertions(+), 9 deletions(-) create mode 100644 src/main/java/com/example/rest/Exceptions/ApiException.java create mode 100644 src/main/java/com/example/rest/Exceptions/ApiExceptionHandler.java create mode 100644 src/main/java/com/example/rest/Exceptions/ApiRequestException.java diff --git a/pom.xml b/pom.xml index 00039f7..e0bf121 100644 --- a/pom.xml +++ b/pom.xml @@ -25,7 +25,6 @@ org.springframework.boot spring-boot-starter-web - org.springframework.boot spring-boot-devtools @@ -47,6 +46,17 @@ spring-boot-starter-test test + + + + + org.springdoc + springdoc-openapi-starter-webmvc-ui + 2.5.0 + + + + diff --git a/src/main/java/com/example/rest/DTO/ProjectDTO.java b/src/main/java/com/example/rest/DTO/ProjectDTO.java index c6865db..63238f5 100644 --- a/src/main/java/com/example/rest/DTO/ProjectDTO.java +++ b/src/main/java/com/example/rest/DTO/ProjectDTO.java @@ -6,7 +6,7 @@ @Component public class ProjectDTO { private String name; - private String description;gi + private String description; public ProjectDTO() { } diff --git a/src/main/java/com/example/rest/Exceptions/ApiException.java b/src/main/java/com/example/rest/Exceptions/ApiException.java new file mode 100644 index 0000000..f7eceef --- /dev/null +++ b/src/main/java/com/example/rest/Exceptions/ApiException.java @@ -0,0 +1,35 @@ +package com.example.rest.Exceptions; + +import org.springframework.http.HttpStatus; + +import java.time.ZonedDateTime; + +public class ApiException { + private final String message; + private final Throwable throwable; + private final HttpStatus httpStatus; + private final ZonedDateTime timestamp; + + public ApiException(String message, Throwable throwable, HttpStatus httpStatus, ZonedDateTime timestamp) { + this.message = message; + this.throwable = throwable; + this.httpStatus = httpStatus; + this.timestamp = timestamp; + } + + public String getMessage() { + return message; + } + + public Throwable getThrowable() { + return throwable; + } + + public HttpStatus getHttpStatus() { + return httpStatus; + } + + public ZonedDateTime getTimestamp() { + return timestamp; + } +} diff --git a/src/main/java/com/example/rest/Exceptions/ApiExceptionHandler.java b/src/main/java/com/example/rest/Exceptions/ApiExceptionHandler.java new file mode 100644 index 0000000..fd2f1e1 --- /dev/null +++ b/src/main/java/com/example/rest/Exceptions/ApiExceptionHandler.java @@ -0,0 +1,20 @@ +package com.example.rest.Exceptions; + +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ControllerAdvice; +import org.springframework.web.bind.annotation.ExceptionHandler; + +import java.time.ZoneId; +import java.time.ZonedDateTime; + +@ControllerAdvice +public class ApiExceptionHandler { + + @ExceptionHandler(value = {ApiRequestException.class}) + public ResponseEntity handleApiRequestException (ApiRequestException e) { + HttpStatus badRequest = HttpStatus.BAD_REQUEST; + ApiException apiException = new ApiException(e.getMessage(), e, badRequest, ZonedDateTime.now(ZoneId.of("Z"))); + return new ResponseEntity<>(apiException, badRequest); + } +} diff --git a/src/main/java/com/example/rest/Exceptions/ApiRequestException.java b/src/main/java/com/example/rest/Exceptions/ApiRequestException.java new file mode 100644 index 0000000..50753f4 --- /dev/null +++ b/src/main/java/com/example/rest/Exceptions/ApiRequestException.java @@ -0,0 +1,11 @@ +package com.example.rest.Exceptions; + +public class ApiRequestException extends RuntimeException { + public ApiRequestException(String message) { + super(message); + } + + public ApiRequestException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/src/main/java/com/example/rest/Services/ProjectServiceImpl.java b/src/main/java/com/example/rest/Services/ProjectServiceImpl.java index b8db108..7902443 100644 --- a/src/main/java/com/example/rest/Services/ProjectServiceImpl.java +++ b/src/main/java/com/example/rest/Services/ProjectServiceImpl.java @@ -1,6 +1,7 @@ package com.example.rest.Services; import com.example.rest.DTO.ProjectDTO; +import com.example.rest.Exceptions.ApiRequestException; import com.example.rest.entities.Project; import com.example.rest.entities.projectStatus; import com.example.rest.repositories.ProjectRepository; @@ -46,7 +47,7 @@ public Project editProject(Long id, Project project) { return project; } else { - throw new RuntimeException("El proyecto no existe"); // Cambiar por exp personalizada + throw new ApiRequestException("El proyecto no existe con expresion personalizada"); } } @@ -56,7 +57,7 @@ public void deleteProject(Long id) { if(projectOptional.isPresent()){ projectRepository.deleteById(id); } else { - throw new RuntimeException("Proyecto con el id: " + id + " no existe."); + throw new ApiRequestException("Proyecto con el id: " + id + " no existe."); } } @@ -66,7 +67,7 @@ public Project getProjectById(Long id) { if(projectOptional.isPresent()) { return projectOptional.get(); } else { - throw new RuntimeException("Proyecto con el id: " + id + " no existe."); + throw new ApiRequestException("Proyecto con el id: " + id + " no existe."); } } diff --git a/src/main/java/com/example/rest/Services/TaskServiceImpl.java b/src/main/java/com/example/rest/Services/TaskServiceImpl.java index fe5111d..7b3bd9d 100644 --- a/src/main/java/com/example/rest/Services/TaskServiceImpl.java +++ b/src/main/java/com/example/rest/Services/TaskServiceImpl.java @@ -1,6 +1,7 @@ package com.example.rest.Services; import com.example.rest.DTO.TaskDTO; +import com.example.rest.Exceptions.ApiRequestException; import com.example.rest.entities.Project; import com.example.rest.entities.Task; import com.example.rest.entities.TaskStatus; @@ -28,7 +29,7 @@ public Task createTask(long project_id, TaskDTO taskDTO) { Project proyectoBaseDeDatos = projectRepository.findProjectById(project_id); if (proyectoBaseDeDatos != null) { - throw new RuntimeException("Ya existe en la base de datos"); + throw new ApiRequestException("Ya existe en la base de datos"); } Task newTask = new Task(); @@ -51,7 +52,7 @@ public void deleteTask(Long task_id) { if(taskOptional.isPresent()){ taskRepository.deleteById(task_id); } else { - throw new RuntimeException("La tarea no existe"); // cambiar + throw new ApiRequestException("La tarea no existe"); } } @@ -61,7 +62,7 @@ public Task getTaskById(Long task_id) { if(taskOptional.isPresent()){ return taskOptional.get(); } else { - throw new RuntimeException("La tarea no existe"); // cambiar + throw new ApiRequestException("La tarea no existe"); } } } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 4bda246..84c9426 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -7,4 +7,7 @@ spring.datasource.password=Hola123@123 spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=true -spring.jpa.properties.hibernate.format_sql=true \ No newline at end of file +spring.jpa.properties.hibernate.format_sql=true + +#Configuracion de Swagger +# http://localhost:8080/swagger-ui/index.html \ No newline at end of file