Skip to content

Commit dea88da

Browse files
committed
Adding dao dependencies and exception handling of original classes
1 parent 8a307c1 commit dea88da

File tree

4 files changed

+28
-63
lines changed

4 files changed

+28
-63
lines changed

spring-boot-rest/pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,14 @@
2424
<groupId>com.fasterxml.jackson.dataformat</groupId>
2525
<artifactId>jackson-dataformat-xml</artifactId>
2626
</dependency>
27+
<dependency>
28+
<groupId>org.hibernate</groupId>
29+
<artifactId>hibernate-entitymanager</artifactId>
30+
</dependency>
31+
<dependency>
32+
<groupId>org.springframework</groupId>
33+
<artifactId>spring-jdbc</artifactId>
34+
</dependency>
2735

2836
<dependency>
2937
<groupId>org.springframework.boot</groupId>

spring-boot-rest/src/main/java/com/baeldung/web/error/RestResponseEntityExceptionHandler.java

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
package com.baeldung.web.error;
22

3+
import javax.persistence.EntityNotFoundException;
4+
5+
import org.hibernate.exception.ConstraintViolationException;
6+
import org.springframework.dao.DataAccessException;
7+
import org.springframework.dao.DataIntegrityViolationException;
8+
import org.springframework.dao.InvalidDataAccessApiUsageException;
39
import org.springframework.http.HttpHeaders;
410
import org.springframework.http.HttpStatus;
511
import org.springframework.http.ResponseEntity;
@@ -10,8 +16,6 @@
1016
import org.springframework.web.context.request.WebRequest;
1117
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
1218

13-
import com.baeldung.web.exception.MyDataAccessException;
14-
import com.baeldung.web.exception.MyDataIntegrityViolationException;
1519
import com.baeldung.web.exception.MyResourceNotFoundException;
1620

1721
@ControllerAdvice
@@ -24,13 +28,15 @@ public RestResponseEntityExceptionHandler() {
2428
// API
2529

2630
// 400
27-
/*
28-
* Some examples of exceptions that we could retrieve as 400 (BAD_REQUEST) responses:
29-
* Hibernate's ConstraintViolationException
30-
* Spring's DataIntegrityViolationException
31-
*/
32-
@ExceptionHandler({ MyDataIntegrityViolationException.class })
33-
public ResponseEntity<Object> handleBadRequest(final MyDataIntegrityViolationException ex, final WebRequest request) {
31+
32+
@ExceptionHandler({ ConstraintViolationException.class })
33+
public ResponseEntity<Object> handleBadRequest(final ConstraintViolationException ex, final WebRequest request) {
34+
final String bodyOfResponse = "This should be application specific";
35+
return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.BAD_REQUEST, request);
36+
}
37+
38+
@ExceptionHandler({ DataIntegrityViolationException.class })
39+
public ResponseEntity<Object> handleBadRequest(final DataIntegrityViolationException ex, final WebRequest request) {
3440
final String bodyOfResponse = "This should be application specific";
3541
return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.BAD_REQUEST, request);
3642
}
@@ -50,23 +56,16 @@ protected ResponseEntity<Object> handleMethodArgumentNotValid(final MethodArgume
5056

5157

5258
// 404
53-
/*
54-
* Some examples of exceptions that we could retrieve as 404 (NOT_FOUND) responses:
55-
* Java Persistence's EntityNotFoundException
56-
*/
57-
@ExceptionHandler(value = { MyResourceNotFoundException.class })
59+
60+
@ExceptionHandler(value = { EntityNotFoundException.class, MyResourceNotFoundException.class })
5861
protected ResponseEntity<Object> handleNotFound(final RuntimeException ex, final WebRequest request) {
5962
final String bodyOfResponse = "This should be application specific";
6063
return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.NOT_FOUND, request);
6164
}
6265

6366
// 409
64-
/*
65-
* Some examples of exceptions that we could retrieve as 409 (CONFLICT) responses:
66-
* Spring's InvalidDataAccessApiUsageException
67-
* Spring's DataAccessException
68-
*/
69-
@ExceptionHandler({ MyDataAccessException.class})
67+
68+
@ExceptionHandler({ InvalidDataAccessApiUsageException.class, DataAccessException.class })
7069
protected ResponseEntity<Object> handleConflict(final RuntimeException ex, final WebRequest request) {
7170
final String bodyOfResponse = "This should be application specific";
7271
return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.CONFLICT, request);
@@ -83,4 +82,4 @@ protected ResponseEntity<Object> handleConflict(final RuntimeException ex, final
8382
return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.INTERNAL_SERVER_ERROR, request);
8483
}
8584

86-
}
85+
}

spring-boot-rest/src/main/java/com/baeldung/web/exception/MyDataAccessException.java

Lines changed: 0 additions & 21 deletions
This file was deleted.

spring-boot-rest/src/main/java/com/baeldung/web/exception/MyDataIntegrityViolationException.java

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)