From adb5599c5e55d7dd4462258e4509511d76eade81 Mon Sep 17 00:00:00 2001 From: Michael Ernst Date: Tue, 12 Dec 2017 19:37:00 +0100 Subject: [PATCH] 24 added support to the RestExceptionHandler to access the HttpServletResponse --- CHANGELOG.adoc | 3 +++ .../exhandler/RestHandlerExceptionResolver.java | 6 +++--- .../exhandler/handlers/RestExceptionHandler.java | 14 ++++++++++++++ 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index c543d0d..5a80c01 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -2,6 +2,9 @@ :repo-uri: https://github.com/jirutka/spring-rest-exception-handler :issue-uri: {repo-uri}/issues +== 1.3.0 (2017-12-13) +* Added support to access the `HttpServletResponse` in + == 1.2.0 (2015-05-16) * Modify `ErrorMessageRestExceptionHandler` to log missing message on the DEBUG level instead of INFO. diff --git a/src/main/java/cz/jirutka/spring/exhandler/RestHandlerExceptionResolver.java b/src/main/java/cz/jirutka/spring/exhandler/RestHandlerExceptionResolver.java index e796820..c6a17e0 100644 --- a/src/main/java/cz/jirutka/spring/exhandler/RestHandlerExceptionResolver.java +++ b/src/main/java/cz/jirutka/spring/exhandler/RestHandlerExceptionResolver.java @@ -115,7 +115,7 @@ protected ModelAndView doResolveException( ResponseEntity entity; try { - entity = handleException(exception, request); + entity = handleException(exception, request, response); } catch (NoExceptionHandlerFoundException ex) { LOG.warn("No exception handler found to handle exception: {}", exception.getClass().getName()); return null; @@ -129,7 +129,7 @@ protected ModelAndView doResolveException( return new ModelAndView(); } - protected ResponseEntity handleException(Exception exception, HttpServletRequest request) { + protected ResponseEntity handleException(Exception exception, HttpServletRequest request, HttpServletResponse response) { // See http://stackoverflow.com/a/12979543/2217862 // This attribute is never set in MockMvc, so it's not covered in integration test. request.removeAttribute(PRODUCIBLE_MEDIA_TYPES_ATTRIBUTE); @@ -137,7 +137,7 @@ protected ResponseEntity handleException(Exception exception, HttpServletRequ RestExceptionHandler handler = resolveExceptionHandler(exception.getClass()); LOG.debug("Handling exception {} with response factory: {}", exception.getClass().getName(), handler); - return handler.handleException(exception, request); + return handler.handleException(exception, request, response); } @SuppressWarnings("unchecked") diff --git a/src/main/java/cz/jirutka/spring/exhandler/handlers/RestExceptionHandler.java b/src/main/java/cz/jirutka/spring/exhandler/handlers/RestExceptionHandler.java index 315b8ef..c5d0493 100644 --- a/src/main/java/cz/jirutka/spring/exhandler/handlers/RestExceptionHandler.java +++ b/src/main/java/cz/jirutka/spring/exhandler/handlers/RestExceptionHandler.java @@ -18,6 +18,7 @@ import org.springframework.http.ResponseEntity; import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; /** * Contract for classes generating a {@link ResponseEntity} for an instance of the specified @@ -37,4 +38,17 @@ public interface RestExceptionHandler { * @return A response entity. */ ResponseEntity handleException(E exception, HttpServletRequest request); + + + /** + * Handles exception and generates {@link ResponseEntity}. + * + * @param exception The exception to handle and get data from. + * @param request The current request. + * @param response The current response. + * @return A response entity. + */ + default ResponseEntity handleException(E exception, HttpServletRequest request, HttpServletResponse response) { + return handleException(exception, request); + } }