Handling Exceptions Before Spring 3
Before Spring 3, exceptions were handled with HandlerExceptionResolvers. This interface defines a single method:ModelAndView resolveException( HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
For the sake of this example, two fake CustomizedException1 and CustomizedException2 exceptions have been created. To map customized exceptions to views, one could (and can still) use a SimpleMappingExceptionResolver:
SimpleMappingExceptionResolver getSimpleMappingExceptionResolver() { SimpleMappingExceptionResolver result = new SimpleMappingExceptionResolver(); // Setting customized exception mappings Properties p = new Properties(); p.put(CustomizedException1.class.getName(), "Errors/Exception1"); result.setExceptionMappings(p); // Unmapped exceptions will be directed there result.setDefaultErrorView("Errors/Default"); // Setting a default HTTP status code result.setDefaultStatusCode(HttpStatus.BAD_REQUEST.value()); return result; }
Here is the Exception1 JSP page, the default page is similar:
<%@page contentType="text/html" pageEncoding="UTF-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <!doctype html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html;" charset=UTF-8"> <title>Welcome To Exception I !!!</title> </head> <body> <h1>Welcome To Exception I !!!</h1> Exception special message:< ${exception.specialMsg} <a href="<c:url value='/'/>">Home</a> </body> </html>
@Controller public class TriggeringErrorsController { @RequestMapping(value = "/throwCustomizedException1") public ModelAndView throwCustomizedException1( HttpServletRequest request,HttpServletResponse response) throws CustomizedException1 { throw new CustomizedException1( "Houston, we have a problem!"); } @RequestMapping(value = "/throwCustomizedException2") public ModelAndView throwCustomizedException2( HttpServletRequest request,HttpServletResponse response) throws CustomizedException2 { throw new CustomizedException2( "Something happened on the way to heaven!"); } ... }
We also configure a target page for HTTP status codes in web.xml, which is an other way to deal with issues:
<error-page> <error-code>404</error-code> <location>/WEB-INF/pages/Errors/My404.jsp</location> </error-page>
What Is New Since Spring 3.X?
The @ResponseStatus annotation is a new mean to set a Http status code when a method is invoked. These are handled by the ResponseStatusExceptionResolver. The @ExceptionHandler annotation facilitates the handling of exceptions in Spring. Such annotations are processed by the AnnotationMethodHandlerExceptionResolver.The following illustrates how these annotations can be used to set an HTTP status code to the response when our customized exception is triggered. The message is returned in the response's body:
@Controller public class TriggeringErrorsController { ... @ExceptionHandler(Customized4ExceptionHandler.class) @ResponseStatus(value=HttpStatus.BAD_REQUEST) @ResponseBody public String handleCustomized4Exception( Customized4ExceptionHandler ex) { return ex.getSpecialMsg(); } @RequestMapping(value = "/throwCustomized4ExceptionHandler") public ModelAndView throwCustomized4ExceptionHandler( HttpServletRequest request,HttpServletResponse response) throws Customized4ExceptionHandler { throw new Customized4ExceptionHandler("S.O.S !!!!"); } }
$.ajax({ type: 'GET', url: prefix + "/throwCustomized4ExceptionHandler", async: true, success: function(result) { alert('Unexpected success !!!'); }, error: function(jqXHR, textStatus, errorThrown) { alert(jqXHR.status + " " + jqXHR.responseText); } });
Since we are using several resolvers, we need a composite resolver (as mentioned earlier):
@Configuration public class ErrorHandling { ... @Bean HandlerExceptionResolverComposite getHandlerExceptionResolverComposite() { HandlerExceptionResolverComposite result = new HandlerExceptionResolverComposite(); List<HandlerExceptionResolver> l = new ArrayList<HandlerExceptionResolver>(); l.add(new AnnotationMethodHandlerExceptionResolver()); l.add(new ResponseStatusExceptionResolver()); l.add(getSimpleMappingExceptionResolver()); l.add(new DefaultHandlerExceptionResolver()); result.setExceptionResolvers(l); return result; }
Running The Example
Once compiled, the example can be run with mvn tomcat:run. Then, browse:http://localhost:8585/spring-mvc-error-handling/
The main page will look like this:
If you click on the Exception 1 link, the following page will display:
If you click on the Exception 2 link, the following page will display:
If you click on the Exception Handler button, a pop-up will be displayed:
These techniques are enough to cover error handling in Spring.
More Spring related posts here.
No comments:
Post a Comment