Skip to content

Commit 22e98e0

Browse files
committed
Removed most exception handlers from spring-rest-full (except the ones used in other articles) and moved them to spring-boot-rest
1 parent 4d90ca6 commit 22e98e0

File tree

9 files changed

+154
-107
lines changed

9 files changed

+154
-107
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package com.baeldung.web.error;
2+
3+
import org.springframework.http.HttpHeaders;
4+
import org.springframework.http.HttpStatus;
5+
import org.springframework.http.ResponseEntity;
6+
import org.springframework.http.converter.HttpMessageNotReadableException;
7+
import org.springframework.web.bind.MethodArgumentNotValidException;
8+
import org.springframework.web.bind.annotation.ControllerAdvice;
9+
import org.springframework.web.bind.annotation.ExceptionHandler;
10+
import org.springframework.web.context.request.WebRequest;
11+
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
12+
13+
import com.baeldung.web.exception.MyDataAccessException;
14+
import com.baeldung.web.exception.MyDataIntegrityViolationException;
15+
import com.baeldung.web.exception.MyResourceNotFoundException;
16+
17+
@ControllerAdvice
18+
public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
19+
20+
public RestResponseEntityExceptionHandler() {
21+
super();
22+
}
23+
24+
// API
25+
26+
// 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) {
34+
final String bodyOfResponse = "This should be application specific";
35+
return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.BAD_REQUEST, request);
36+
}
37+
38+
@Override
39+
protected ResponseEntity<Object> handleHttpMessageNotReadable(final HttpMessageNotReadableException ex, final HttpHeaders headers, final HttpStatus status, final WebRequest request) {
40+
final String bodyOfResponse = "This should be application specific";
41+
// ex.getCause() instanceof JsonMappingException, JsonParseException // for additional information later on
42+
return handleExceptionInternal(ex, bodyOfResponse, headers, HttpStatus.BAD_REQUEST, request);
43+
}
44+
45+
@Override
46+
protected ResponseEntity<Object> handleMethodArgumentNotValid(final MethodArgumentNotValidException ex, final HttpHeaders headers, final HttpStatus status, final WebRequest request) {
47+
final String bodyOfResponse = "This should be application specific";
48+
return handleExceptionInternal(ex, bodyOfResponse, headers, HttpStatus.BAD_REQUEST, request);
49+
}
50+
51+
52+
// 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 })
58+
protected ResponseEntity<Object> handleNotFound(final RuntimeException ex, final WebRequest request) {
59+
final String bodyOfResponse = "This should be application specific";
60+
return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.NOT_FOUND, request);
61+
}
62+
63+
// 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})
70+
protected ResponseEntity<Object> handleConflict(final RuntimeException ex, final WebRequest request) {
71+
final String bodyOfResponse = "This should be application specific";
72+
return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.CONFLICT, request);
73+
}
74+
75+
// 412
76+
77+
// 500
78+
79+
@ExceptionHandler({ NullPointerException.class, IllegalArgumentException.class, IllegalStateException.class })
80+
/*500*/public ResponseEntity<Object> handleInternal(final RuntimeException ex, final WebRequest request) {
81+
logger.error("500 Status Code", ex);
82+
final String bodyOfResponse = "This should be application specific";
83+
return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.INTERNAL_SERVER_ERROR, request);
84+
}
85+
86+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.web.exception;
2+
3+
public final class MyDataAccessException extends RuntimeException {
4+
5+
public MyDataAccessException() {
6+
super();
7+
}
8+
9+
public MyDataAccessException(final String message, final Throwable cause) {
10+
super(message, cause);
11+
}
12+
13+
public MyDataAccessException(final String message) {
14+
super(message);
15+
}
16+
17+
public MyDataAccessException(final Throwable cause) {
18+
super(cause);
19+
}
20+
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.web.exception;
2+
3+
public final class MyDataIntegrityViolationException extends RuntimeException {
4+
5+
public MyDataIntegrityViolationException() {
6+
super();
7+
}
8+
9+
public MyDataIntegrityViolationException(final String message, final Throwable cause) {
10+
super(message, cause);
11+
}
12+
13+
public MyDataIntegrityViolationException(final String message) {
14+
super(message);
15+
}
16+
17+
public MyDataIntegrityViolationException(final Throwable cause) {
18+
super(cause);
19+
}
20+
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.web.exception;
2+
3+
public final class MyResourceNotFoundException extends RuntimeException {
4+
5+
public MyResourceNotFoundException() {
6+
super();
7+
}
8+
9+
public MyResourceNotFoundException(final String message, final Throwable cause) {
10+
super(message, cause);
11+
}
12+
13+
public MyResourceNotFoundException(final String message) {
14+
super(message);
15+
}
16+
17+
public MyResourceNotFoundException(final Throwable cause) {
18+
super(cause);
19+
}
20+
21+
}

spring-rest-full/.attach_pid28499

Whitespace-only changes.

spring-rest-full/pom.xml

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -212,23 +212,6 @@
212212
<groupId>org.apache.maven.plugins</groupId>
213213
<artifactId>maven-war-plugin</artifactId>
214214
</plugin>
215-
<!-- Because we are using custom surefire configs in live profile hence need to disable all other in default profile -->
216-
<plugin>
217-
<groupId>org.apache.maven.plugins</groupId>
218-
<artifactId>maven-surefire-plugin</artifactId>
219-
<configuration>
220-
<forkCount>3</forkCount>
221-
<reuseForks>true</reuseForks>
222-
<excludes>
223-
<exclude>**/*IntegrationTest.java</exclude>
224-
<exclude>**/*IntTest.java</exclude>
225-
<exclude>**/*LongRunningUnitTest.java</exclude>
226-
<exclude>**/*ManualTest.java</exclude>
227-
<exclude>**/*LiveTest.java</exclude>
228-
<exclude>**/*TestSuite.java</exclude>
229-
</excludes>
230-
</configuration>
231-
</plugin>
232215
<plugin>
233216
<groupId>org.codehaus.cargo</groupId>
234217
<artifactId>cargo-maven2-plugin</artifactId>
@@ -274,32 +257,6 @@
274257
<id>live</id>
275258
<build>
276259
<plugins>
277-
<plugin>
278-
<groupId>org.apache.maven.plugins</groupId>
279-
<artifactId>maven-surefire-plugin</artifactId>
280-
<executions>
281-
<execution>
282-
<phase>integration-test</phase>
283-
<goals>
284-
<goal>test</goal>
285-
</goals>
286-
<configuration>
287-
<excludes>
288-
<exclude>**/*IntegrationTest.java</exclude>
289-
<exclude>**/*IntTest.java</exclude>
290-
</excludes>
291-
<includes>
292-
<include>**/*LiveTest.java</include>
293-
</includes>
294-
</configuration>
295-
</execution>
296-
</executions>
297-
<configuration>
298-
<systemPropertyVariables>
299-
<test.mime>json</test.mime>
300-
</systemPropertyVariables>
301-
</configuration>
302-
</plugin>
303260
<plugin>
304261
<groupId>org.codehaus.cargo</groupId>
305262
<artifactId>cargo-maven2-plugin</artifactId>
Lines changed: 1 addition & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,9 @@
11
package org.baeldung.web.error;
22

3-
import javax.persistence.EntityNotFoundException;
4-
53
import org.baeldung.web.exception.MyResourceNotFoundException;
6-
import org.hibernate.exception.ConstraintViolationException;
7-
import org.springframework.dao.DataAccessException;
8-
import org.springframework.dao.DataIntegrityViolationException;
9-
import org.springframework.dao.InvalidDataAccessApiUsageException;
104
import org.springframework.http.HttpHeaders;
115
import org.springframework.http.HttpStatus;
126
import org.springframework.http.ResponseEntity;
13-
import org.springframework.http.converter.HttpMessageNotReadableException;
14-
import org.springframework.web.bind.MethodArgumentNotValidException;
157
import org.springframework.web.bind.annotation.ControllerAdvice;
168
import org.springframework.web.bind.annotation.ExceptionHandler;
179
import org.springframework.web.context.request.WebRequest;
@@ -24,61 +16,10 @@ public RestResponseEntityExceptionHandler() {
2416
super();
2517
}
2618

27-
// API
28-
29-
// 400
30-
31-
@ExceptionHandler({ ConstraintViolationException.class })
32-
public ResponseEntity<Object> handleBadRequest(final ConstraintViolationException ex, final WebRequest request) {
33-
final String bodyOfResponse = "This should be application specific";
34-
return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.BAD_REQUEST, request);
35-
}
36-
37-
@ExceptionHandler({ DataIntegrityViolationException.class })
38-
public ResponseEntity<Object> handleBadRequest(final DataIntegrityViolationException ex, final WebRequest request) {
39-
final String bodyOfResponse = "This should be application specific";
40-
return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.BAD_REQUEST, request);
41-
}
42-
43-
@Override
44-
protected ResponseEntity<Object> handleHttpMessageNotReadable(final HttpMessageNotReadableException ex, final HttpHeaders headers, final HttpStatus status, final WebRequest request) {
45-
final String bodyOfResponse = "This should be application specific";
46-
// ex.getCause() instanceof JsonMappingException, JsonParseException // for additional information later on
47-
return handleExceptionInternal(ex, bodyOfResponse, headers, HttpStatus.BAD_REQUEST, request);
48-
}
49-
50-
@Override
51-
protected ResponseEntity<Object> handleMethodArgumentNotValid(final MethodArgumentNotValidException ex, final HttpHeaders headers, final HttpStatus status, final WebRequest request) {
52-
final String bodyOfResponse = "This should be application specific";
53-
return handleExceptionInternal(ex, bodyOfResponse, headers, HttpStatus.BAD_REQUEST, request);
54-
}
55-
56-
57-
// 404
58-
59-
@ExceptionHandler(value = { EntityNotFoundException.class, MyResourceNotFoundException.class })
19+
@ExceptionHandler(value = { MyResourceNotFoundException.class })
6020
protected ResponseEntity<Object> handleNotFound(final RuntimeException ex, final WebRequest request) {
6121
final String bodyOfResponse = "This should be application specific";
6222
return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.NOT_FOUND, request);
6323
}
6424

65-
// 409
66-
67-
@ExceptionHandler({ InvalidDataAccessApiUsageException.class, DataAccessException.class })
68-
protected ResponseEntity<Object> handleConflict(final RuntimeException ex, final WebRequest request) {
69-
final String bodyOfResponse = "This should be application specific";
70-
return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.CONFLICT, request);
71-
}
72-
73-
// 412
74-
75-
// 500
76-
77-
@ExceptionHandler({ NullPointerException.class, IllegalArgumentException.class, IllegalStateException.class })
78-
/*500*/public ResponseEntity<Object> handleInternal(final RuntimeException ex, final WebRequest request) {
79-
logger.error("500 Status Code", ex);
80-
final String bodyOfResponse = "This should be application specific";
81-
return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.INTERNAL_SERVER_ERROR, request);
82-
}
83-
8425
}
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
package org.baeldung;
22

33
import org.baeldung.persistence.PersistenceTestSuite;
4-
import org.baeldung.web.LiveTestSuite;
4+
import org.baeldung.web.LiveTestSuiteLiveTest;
55
import org.junit.runner.RunWith;
66
import org.junit.runners.Suite;
77

88
@RunWith(Suite.class)
99
@Suite.SuiteClasses({
1010
// @formatter:off
1111
PersistenceTestSuite.class
12-
,LiveTestSuite.class
12+
,LiveTestSuiteLiveTest.class
1313
}) //
14-
public class TestSuite {
14+
public class TestSuiteLiveTest {
1515

1616
}

spring-rest-full/src/test/java/org/baeldung/web/LiveTestSuite.java renamed to spring-rest-full/src/test/java/org/baeldung/web/LiveTestSuiteLiveTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010
,FooLiveTest.class
1111
,FooPageableLiveTest.class
1212
}) //
13-
public class LiveTestSuite {
13+
public class LiveTestSuiteLiveTest {
1414

1515
}

0 commit comments

Comments
 (0)