Skip to content

Commit

Permalink
Swagger and Exception Handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Pezcue committed Apr 14, 2024
1 parent 376cab6 commit 6064d54
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 9 deletions.
12 changes: 11 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
Expand All @@ -47,6 +46,17 @@
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<!-- Swagger-->
<!-- https://mvnrepository.com/artifact/org.springdoc/springdoc-openapi-starter-webmvc-ui -->
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.5.0</version>
</dependency>



</dependencies>

<build>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/example/rest/DTO/ProjectDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
@Component
public class ProjectDTO {
private String name;
private String description;gi
private String description;

public ProjectDTO() {
}
Expand Down
35 changes: 35 additions & 0 deletions src/main/java/com/example/rest/Exceptions/ApiException.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
20 changes: 20 additions & 0 deletions src/main/java/com/example/rest/Exceptions/ApiExceptionHandler.java
Original file line number Diff line number Diff line change
@@ -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<Object> 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);
}
}
11 changes: 11 additions & 0 deletions src/main/java/com/example/rest/Exceptions/ApiRequestException.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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");
}
}

Expand All @@ -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.");
}
}

Expand All @@ -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.");
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/main/java/com/example/rest/Services/TaskServiceImpl.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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();
Expand All @@ -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");
}
}

Expand All @@ -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");
}
}
}
5 changes: 4 additions & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
spring.jpa.properties.hibernate.format_sql=true

#Configuracion de Swagger
# http://localhost:8080/swagger-ui/index.html

0 comments on commit 6064d54

Please sign in to comment.