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 new file mode 100644 index 0000000000..fe0465156d --- /dev/null +++ b/spring-boot-rest/src/main/java/com/baeldung/web/error/RestResponseEntityExceptionHandler.java @@ -0,0 +1,86 @@ +package com.baeldung.web.error; + +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.http.converter.HttpMessageNotReadableException; +import org.springframework.web.bind.MethodArgumentNotValidException; +import org.springframework.web.bind.annotation.ControllerAdvice; +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 +public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler { + + public RestResponseEntityExceptionHandler() { + super(); + } + + // 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) { + final String bodyOfResponse = "This should be application specific"; + return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.BAD_REQUEST, request); + } + + @Override + protected ResponseEntity handleHttpMessageNotReadable(final HttpMessageNotReadableException ex, final HttpHeaders headers, final HttpStatus status, final WebRequest request) { + final String bodyOfResponse = "This should be application specific"; + // ex.getCause() instanceof JsonMappingException, JsonParseException // for additional information later on + return handleExceptionInternal(ex, bodyOfResponse, headers, HttpStatus.BAD_REQUEST, request); + } + + @Override + protected ResponseEntity handleMethodArgumentNotValid(final MethodArgumentNotValidException ex, final HttpHeaders headers, final HttpStatus status, final WebRequest request) { + final String bodyOfResponse = "This should be application specific"; + return handleExceptionInternal(ex, bodyOfResponse, headers, HttpStatus.BAD_REQUEST, request); + } + + + // 404 + /* + * Some examples of exceptions that we could retrieve as 404 (NOT_FOUND) responses: + * Java Persistence's EntityNotFoundException + */ + @ExceptionHandler(value = { 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}) + 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); + } + + // 412 + + // 500 + + @ExceptionHandler({ NullPointerException.class, IllegalArgumentException.class, IllegalStateException.class }) + /*500*/public ResponseEntity handleInternal(final RuntimeException ex, final WebRequest request) { + logger.error("500 Status Code", ex); + final String bodyOfResponse = "This should be application specific"; + return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.INTERNAL_SERVER_ERROR, request); + } + +} 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 new file mode 100644 index 0000000000..8fc9a3a0ea --- /dev/null +++ b/spring-boot-rest/src/main/java/com/baeldung/web/exception/MyDataAccessException.java @@ -0,0 +1,21 @@ +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 new file mode 100644 index 0000000000..50adb09c09 --- /dev/null +++ b/spring-boot-rest/src/main/java/com/baeldung/web/exception/MyDataIntegrityViolationException.java @@ -0,0 +1,21 @@ +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); + } + +} diff --git a/spring-boot-rest/src/main/java/com/baeldung/web/exception/MyResourceNotFoundException.java b/spring-boot-rest/src/main/java/com/baeldung/web/exception/MyResourceNotFoundException.java new file mode 100644 index 0000000000..fd002efc28 --- /dev/null +++ b/spring-boot-rest/src/main/java/com/baeldung/web/exception/MyResourceNotFoundException.java @@ -0,0 +1,21 @@ +package com.baeldung.web.exception; + +public final class MyResourceNotFoundException extends RuntimeException { + + public MyResourceNotFoundException() { + super(); + } + + public MyResourceNotFoundException(final String message, final Throwable cause) { + super(message, cause); + } + + public MyResourceNotFoundException(final String message) { + super(message); + } + + public MyResourceNotFoundException(final Throwable cause) { + super(cause); + } + +} diff --git a/spring-rest-full/.attach_pid28499 b/spring-rest-full/.attach_pid28499 new file mode 100644 index 0000000000..e69de29bb2 diff --git a/spring-rest-full/pom.xml b/spring-rest-full/pom.xml index 81c938a289..ddc7e042b5 100644 --- a/spring-rest-full/pom.xml +++ b/spring-rest-full/pom.xml @@ -212,23 +212,6 @@ org.apache.maven.plugins maven-war-plugin - - - org.apache.maven.plugins - maven-surefire-plugin - - 3 - true - - **/*IntegrationTest.java - **/*IntTest.java - **/*LongRunningUnitTest.java - **/*ManualTest.java - **/*LiveTest.java - **/*TestSuite.java - - - org.codehaus.cargo cargo-maven2-plugin @@ -274,32 +257,6 @@ live - - org.apache.maven.plugins - maven-surefire-plugin - - - integration-test - - test - - - - **/*IntegrationTest.java - **/*IntTest.java - - - **/*LiveTest.java - - - - - - - json - - - org.codehaus.cargo cargo-maven2-plugin diff --git a/spring-rest-full/src/main/java/org/baeldung/web/error/RestResponseEntityExceptionHandler.java b/spring-rest-full/src/main/java/org/baeldung/web/error/RestResponseEntityExceptionHandler.java index b593116c4a..c0639acef4 100644 --- a/spring-rest-full/src/main/java/org/baeldung/web/error/RestResponseEntityExceptionHandler.java +++ b/spring-rest-full/src/main/java/org/baeldung/web/error/RestResponseEntityExceptionHandler.java @@ -1,17 +1,9 @@ package org.baeldung.web.error; -import javax.persistence.EntityNotFoundException; - import org.baeldung.web.exception.MyResourceNotFoundException; -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; -import org.springframework.http.converter.HttpMessageNotReadableException; -import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.context.request.WebRequest; @@ -24,61 +16,10 @@ public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionH super(); } - // API - - // 400 - - @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); - } - - @Override - protected ResponseEntity handleHttpMessageNotReadable(final HttpMessageNotReadableException ex, final HttpHeaders headers, final HttpStatus status, final WebRequest request) { - final String bodyOfResponse = "This should be application specific"; - // ex.getCause() instanceof JsonMappingException, JsonParseException // for additional information later on - return handleExceptionInternal(ex, bodyOfResponse, headers, HttpStatus.BAD_REQUEST, request); - } - - @Override - protected ResponseEntity handleMethodArgumentNotValid(final MethodArgumentNotValidException ex, final HttpHeaders headers, final HttpStatus status, final WebRequest request) { - final String bodyOfResponse = "This should be application specific"; - return handleExceptionInternal(ex, bodyOfResponse, headers, HttpStatus.BAD_REQUEST, request); - } - - - // 404 - - @ExceptionHandler(value = { EntityNotFoundException.class, MyResourceNotFoundException.class }) + @ExceptionHandler(value = { 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 - - @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); - } - - // 412 - - // 500 - - @ExceptionHandler({ NullPointerException.class, IllegalArgumentException.class, IllegalStateException.class }) - /*500*/public ResponseEntity handleInternal(final RuntimeException ex, final WebRequest request) { - logger.error("500 Status Code", ex); - final String bodyOfResponse = "This should be application specific"; - return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.INTERNAL_SERVER_ERROR, request); - } - } diff --git a/spring-rest-full/src/test/java/org/baeldung/TestSuite.java b/spring-rest-full/src/test/java/org/baeldung/TestSuiteLiveTest.java similarity index 68% rename from spring-rest-full/src/test/java/org/baeldung/TestSuite.java rename to spring-rest-full/src/test/java/org/baeldung/TestSuiteLiveTest.java index cd5fa4661f..76215bb6e3 100644 --- a/spring-rest-full/src/test/java/org/baeldung/TestSuite.java +++ b/spring-rest-full/src/test/java/org/baeldung/TestSuiteLiveTest.java @@ -1,7 +1,7 @@ package org.baeldung; import org.baeldung.persistence.PersistenceTestSuite; -import org.baeldung.web.LiveTestSuite; +import org.baeldung.web.LiveTestSuiteLiveTest; import org.junit.runner.RunWith; import org.junit.runners.Suite; @@ -9,8 +9,8 @@ import org.junit.runners.Suite; @Suite.SuiteClasses({ // @formatter:off PersistenceTestSuite.class - ,LiveTestSuite.class + ,LiveTestSuiteLiveTest.class }) // -public class TestSuite { +public class TestSuiteLiveTest { } diff --git a/spring-rest-full/src/test/java/org/baeldung/web/LiveTestSuite.java b/spring-rest-full/src/test/java/org/baeldung/web/LiveTestSuiteLiveTest.java similarity index 87% rename from spring-rest-full/src/test/java/org/baeldung/web/LiveTestSuite.java rename to spring-rest-full/src/test/java/org/baeldung/web/LiveTestSuiteLiveTest.java index 6d5b94a686..71a61ed338 100644 --- a/spring-rest-full/src/test/java/org/baeldung/web/LiveTestSuite.java +++ b/spring-rest-full/src/test/java/org/baeldung/web/LiveTestSuiteLiveTest.java @@ -10,6 +10,6 @@ import org.junit.runners.Suite; ,FooLiveTest.class ,FooPageableLiveTest.class }) // -public class LiveTestSuite { +public class LiveTestSuiteLiveTest { }