Configuration-based exception handling

SpringMVC provides an interface to handle exceptions that occur during the execution of controller methods: HandlerExceptionResolver.

The implementation classes of the HandlerExceptionResolver interface are:

  • DefaultHandlerExceptionResolverThis is the default handler, and springMVC has already handled some of the exceptions we’ve encountered before.
  • SimpleMappingExceptionResolverThis allows us to customize exception handling. You can set to return a new view when a specified exception occurs.

Use SimpleMappingExceptionResolver in for springMVC configuration file:

<! - configuration exception handling - > < bean class = "org. Springframework. Web. Servlet. Handler. SimpleMappingExceptionResolver" > < property name="exceptionMappings"> <props> <prop key="java.lang.ArithmeticException">error</prop> </props> </property> </bean>Copy the code
  • A class used in the example to handle arithmetic exceptionsArithmeticException, the value error represents the view that jumps after the exception.

Create a new error. HTML page:

<! DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>error</title> </head> <body> error </body> </ HTML >Copy the code

Next, create an exception:

  @RequestMapping("/testExceptionHandler")
  public String testExceptionHandler() {
      System.out.println(1/0);
      return "success";
  }
Copy the code

Normally this handler will jump to the SUCCESS page, but it has a 1/0 exception, so it will jump to the Error page as configured.

Redeploy, test, visit http://localhost:8080/springmvc/testExceptionHandler:

The error page was jumped successfully.

Storing exception information

ExceptionAttribute is a key that stores exception information. By default, it exists in the current request domain:

<! - configuration exception handling - > < bean class = "org. Springframework. Web. Servlet. Handler. SimpleMappingExceptionResolver" > < property name="exceptionMappings"> <props> <prop key="java.lang.ArithmeticException">error</prop> </props> </property> <! <property name="exceptionAttribute" value="ex"></property> </bean>Copy the code

Ex can then be used in the error page to retrieve exception information.

<! DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>error</title> This error < / head > < body > < p th: text = "${ex}" > < / p > < / body > < / HTML >Copy the code

Redeploy and refresh the page:

Annotation-based exception handling

Springmvc also provides a set of annotations through which you can implement the above exception handling.

Create a controller ExceptionController:

@controlleradvice public class ExceptionController {// @exceptionhandler identifies the current class as the ExceptionHandler component Is used to set the identification method to deal with abnormal @ ExceptionHandler (value = {ArithmeticException. Class, NullPointerException. Class}) public String testException (Exception ex, Model Model) {/ / ex said in current request processing Exception object, Put it in the request field model.adDattribute ("ex", ex); return "error"; }}Copy the code
  • @ControllerAdviceIdentifies the current class as the exception handling component.
  • exRepresents the exception object that occurs in the current request processing, withModelPut it in the request field.

Now commented the processor in the configuration file, redeployment, refresh the http://localhost:8080/springmvc/testExceptionHandler:

Still.