diff --git a/spring-boot-rest/pom.xml b/spring-boot-rest/pom.xml index 3b8cf7e39e..f05d242072 100644 --- a/spring-boot-rest/pom.xml +++ b/spring-boot-rest/pom.xml @@ -24,6 +24,14 @@ com.fasterxml.jackson.dataformat jackson-dataformat-xml + + org.hibernate + hibernate-entitymanager + + + org.springframework + spring-jdbc + org.springframework.boot diff --git a/spring-boot-rest/src/main/java/com/baeldung/web/error/RestResponseEntityExceptionHandler.java b/spring-boot-rest/src/main/java/com/baeldung/web/error/RestResponseEntityExceptionHandler.java index fe0465156d..2e2672f510 100644 --- a/spring-boot-rest/src/main/java/com/baeldung/web/error/RestResponseEntityExceptionHandler.java +++ b/spring-boot-rest/src/main/java/com/baeldung/web/error/RestResponseEntityExceptionHandler.java @@ -1,5 +1,11 @@ package com.baeldung.web.error; +import javax.persistence.EntityNotFoundException; + +import org.hibernate.exception.ConstraintViolationException; +import org.springframework.dao.DataAccessException; +import org.springframework.dao.DataIntegrityViolationException; +import org.springframework.dao.InvalidDataAccessApiUsageException; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; @@ -10,8 +16,6 @@ import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.context.request.WebRequest; import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; -import com.baeldung.web.exception.MyDataAccessException; -import com.baeldung.web.exception.MyDataIntegrityViolationException; import com.baeldung.web.exception.MyResourceNotFoundException; @ControllerAdvice @@ -24,13 +28,15 @@ public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionH // API // 400 - /* - * Some examples of exceptions that we could retrieve as 400 (BAD_REQUEST) responses: - * Hibernate's ConstraintViolationException - * Spring's DataIntegrityViolationException - */ - @ExceptionHandler({ MyDataIntegrityViolationException.class }) - public ResponseEntity handleBadRequest(final MyDataIntegrityViolationException ex, final WebRequest request) { + + @ExceptionHandler({ ConstraintViolationException.class }) + public ResponseEntity handleBadRequest(final ConstraintViolationException ex, final WebRequest request) { + final String bodyOfResponse = "This should be application specific"; + return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.BAD_REQUEST, request); + } + + @ExceptionHandler({ DataIntegrityViolationException.class }) + public ResponseEntity handleBadRequest(final DataIntegrityViolationException ex, final WebRequest request) { final String bodyOfResponse = "This should be application specific"; return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.BAD_REQUEST, request); } @@ -50,23 +56,16 @@ public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionH // 404 - /* - * Some examples of exceptions that we could retrieve as 404 (NOT_FOUND) responses: - * Java Persistence's EntityNotFoundException - */ - @ExceptionHandler(value = { MyResourceNotFoundException.class }) + + @ExceptionHandler(value = { EntityNotFoundException.class, MyResourceNotFoundException.class }) protected ResponseEntity handleNotFound(final RuntimeException ex, final WebRequest request) { final String bodyOfResponse = "This should be application specific"; return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.NOT_FOUND, request); } // 409 - /* - * Some examples of exceptions that we could retrieve as 409 (CONFLICT) responses: - * Spring's InvalidDataAccessApiUsageException - * Spring's DataAccessException - */ - @ExceptionHandler({ MyDataAccessException.class}) + + @ExceptionHandler({ InvalidDataAccessApiUsageException.class, DataAccessException.class }) protected ResponseEntity handleConflict(final RuntimeException ex, final WebRequest request) { final String bodyOfResponse = "This should be application specific"; return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.CONFLICT, request); @@ -83,4 +82,4 @@ public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionH return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.INTERNAL_SERVER_ERROR, request); } -} +} \ No newline at end of file diff --git a/spring-boot-rest/src/main/java/com/baeldung/web/exception/MyDataAccessException.java b/spring-boot-rest/src/main/java/com/baeldung/web/exception/MyDataAccessException.java deleted file mode 100644 index 8fc9a3a0ea..0000000000 --- a/spring-boot-rest/src/main/java/com/baeldung/web/exception/MyDataAccessException.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.baeldung.web.exception; - -public final class MyDataAccessException extends RuntimeException { - - public MyDataAccessException() { - super(); - } - - public MyDataAccessException(final String message, final Throwable cause) { - super(message, cause); - } - - public MyDataAccessException(final String message) { - super(message); - } - - public MyDataAccessException(final Throwable cause) { - super(cause); - } - -} diff --git a/spring-boot-rest/src/main/java/com/baeldung/web/exception/MyDataIntegrityViolationException.java b/spring-boot-rest/src/main/java/com/baeldung/web/exception/MyDataIntegrityViolationException.java deleted file mode 100644 index 50adb09c09..0000000000 --- a/spring-boot-rest/src/main/java/com/baeldung/web/exception/MyDataIntegrityViolationException.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.baeldung.web.exception; - -public final class MyDataIntegrityViolationException extends RuntimeException { - - public MyDataIntegrityViolationException() { - super(); - } - - public MyDataIntegrityViolationException(final String message, final Throwable cause) { - super(message, cause); - } - - public MyDataIntegrityViolationException(final String message) { - super(message); - } - - public MyDataIntegrityViolationException(final Throwable cause) { - super(cause); - } - -}