Skip to content

Commit 932b739

Browse files
author
eugenp
committed
new Spring 3.2 based exception handling mechanism
1 parent 0ebe02a commit 932b739

2 files changed

Lines changed: 38 additions & 3 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.rest.common.web;
2+
3+
import org.rest.common.persistence.exception.EntityNotFoundException;
4+
import org.springframework.dao.DataAccessException;
5+
import org.springframework.dao.DataIntegrityViolationException;
6+
import org.springframework.dao.InvalidDataAccessApiUsageException;
7+
import org.springframework.http.HttpHeaders;
8+
import org.springframework.http.HttpStatus;
9+
import org.springframework.http.ResponseEntity;
10+
import org.springframework.web.bind.annotation.ControllerAdvice;
11+
import org.springframework.web.bind.annotation.ExceptionHandler;
12+
import org.springframework.web.context.request.WebRequest;
13+
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
14+
15+
@ControllerAdvice
16+
public class RestResponseEntityExceptionHandler extends ResponseEntityExceptionHandler {
17+
18+
public RestResponseEntityExceptionHandler() {
19+
super();
20+
}
21+
22+
// API
23+
24+
@ExceptionHandler(value = { IllegalArgumentException.class, IllegalStateException.class, InvalidDataAccessApiUsageException.class, DataIntegrityViolationException.class, DataAccessException.class })
25+
protected ResponseEntity<Object> handleConflict(final RuntimeException ex, final WebRequest request) {
26+
final String bodyOfResponse = "This should be application specific";
27+
return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.CONFLICT, request);
28+
}
29+
30+
@ExceptionHandler(value = { EntityNotFoundException.class })
31+
protected ResponseEntity<Object> handleNotFound(final RuntimeException ex, final WebRequest request) {
32+
final String bodyOfResponse = "This should be application specific";
33+
return handleExceptionInternal(ex, bodyOfResponse, new HttpHeaders(), HttpStatus.NOT_FOUND, request);
34+
}
35+
36+
}

‎common/src/main/java/org/rest/common/web/RestResponseStatusExceptionResolver.java‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,16 @@
99
import org.springframework.dao.DataAccessException;
1010
import org.springframework.dao.DataIntegrityViolationException;
1111
import org.springframework.dao.InvalidDataAccessApiUsageException;
12-
import org.springframework.stereotype.Component;
1312
import org.springframework.web.method.HandlerMethod;
1413
import org.springframework.web.servlet.ModelAndView;
1514
import org.springframework.web.servlet.handler.AbstractHandlerExceptionResolver;
1615

17-
@Component
16+
// @Component
1817
public class RestResponseStatusExceptionResolver extends AbstractHandlerExceptionResolver {
1918

2019
// TODO: Order should be set in a @Configuration rather then hard-coded. It is done here due to POC style of implementation.
2120
public RestResponseStatusExceptionResolver() {
22-
setOrder(-99);
21+
// setOrder(-99);
2322
}
2423

2524
// API

0 commit comments

Comments
 (0)