Adding dao dependencies and exception handling of original classes

This commit is contained in:
Ger Roza 2019-01-19 17:00:30 -02:00
parent 8a307c1aba
commit dea88da777
4 changed files with 28 additions and 63 deletions

View File

@ -24,6 +24,14 @@
<groupId>com.fasterxml.jackson.dataformat</groupId> <groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId> <artifactId>jackson-dataformat-xml</artifactId>
</dependency> </dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>

View File

@ -1,5 +1,11 @@
package com.baeldung.web.error; 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.HttpHeaders;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; 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.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; 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; import com.baeldung.web.exception.MyResourceNotFoundException;
@ControllerAdvice @ControllerAdvice
@ -24,13 +28,15 @@ public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionH
// API // API
// 400 // 400
/*
* Some examples of exceptions that we could retrieve as 400 (BAD_REQUEST) responses: @ExceptionHandler({ ConstraintViolationException.class })
* Hibernate's ConstraintViolationException public ResponseEntity<Object> handleBadRequest(final ConstraintViolationException ex, final WebRequest request) {
* Spring's DataIntegrityViolationException final String bodyOfResponse = "This should be application specific";
*/ return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.BAD_REQUEST, request);
@ExceptionHandler({ MyDataIntegrityViolationException.class }) }
public ResponseEntity<Object> handleBadRequest(final MyDataIntegrityViolationException ex, final WebRequest request) {
@ExceptionHandler({ DataIntegrityViolationException.class })
public ResponseEntity<Object> handleBadRequest(final DataIntegrityViolationException ex, final WebRequest request) {
final String bodyOfResponse = "This should be application specific"; final String bodyOfResponse = "This should be application specific";
return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.BAD_REQUEST, request); return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.BAD_REQUEST, request);
} }
@ -50,23 +56,16 @@ public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionH
// 404 // 404
/*
* Some examples of exceptions that we could retrieve as 404 (NOT_FOUND) responses: @ExceptionHandler(value = { EntityNotFoundException.class, MyResourceNotFoundException.class })
* Java Persistence's EntityNotFoundException
*/
@ExceptionHandler(value = { MyResourceNotFoundException.class })
protected ResponseEntity<Object> handleNotFound(final RuntimeException ex, final WebRequest request) { protected ResponseEntity<Object> handleNotFound(final RuntimeException ex, final WebRequest request) {
final String bodyOfResponse = "This should be application specific"; final String bodyOfResponse = "This should be application specific";
return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.NOT_FOUND, request); return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.NOT_FOUND, request);
} }
// 409 // 409
/*
* Some examples of exceptions that we could retrieve as 409 (CONFLICT) responses: @ExceptionHandler({ InvalidDataAccessApiUsageException.class, DataAccessException.class })
* Spring's InvalidDataAccessApiUsageException
* Spring's DataAccessException
*/
@ExceptionHandler({ MyDataAccessException.class})
protected ResponseEntity<Object> handleConflict(final RuntimeException ex, final WebRequest request) { protected ResponseEntity<Object> handleConflict(final RuntimeException ex, final WebRequest request) {
final String bodyOfResponse = "This should be application specific"; final String bodyOfResponse = "This should be application specific";
return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.CONFLICT, request); 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); return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.INTERNAL_SERVER_ERROR, request);
} }
} }

View File

@ -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);
}
}

View File

@ -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);
}
}