Created
August 30, 2012 12:23
-
-
Save bmchild/3527471 to your computer and use it in GitHub Desktop.
Example Spring MVC 3 Exception Handlers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Controller | |
public class SimpleController { | |
// other controller method omitted | |
@ExceptionHandler(IOException.class) | |
public String handleIOException(IOException ex, HttpServletRequest request) { | |
return ClassUtils.getShortName(ex.getClass()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Controller | |
public class SimpleController { | |
// other controller method omitted | |
@ResponseStatus(value=HttpStatus.FORBIDDEN) | |
@ExceptionHandler(SecurityException.class) | |
public ModelAndView workflowExceptionCaught(SecurityException ex) { | |
return new ModelAndView("common/_error", "errorCode", HttpStatus.FORBIDDEN.value()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Controller | |
public class SimpleController { | |
// other controller method omitted | |
@ExceptionHandler({Exception.class, IOException.class}) | |
public ModelAndView exceptionHandler(Exception e) { | |
LOGGER.error("An error has occurred - " + e.getMessage(), e); | |
ModelAndView mav = new ModelAndView("exception"); | |
mav.addObject("showTrace", false); | |
mav.addObject("exception", e); | |
return mav; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<%-- includes omitted --%> | |
<div> | |
<div id="message" class="errorLabel">Exception Thrown: ${exception.message}</div> | |
<c:if test="${showTrace eq null || showTrace eq true }"> | |
<div id="stackTrace">${exception.stackTrace}</div> | |
</c:if> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment