Get the App
SLTechnology News&Howtos  ›  Development  › 

How to implement SpringMvc exception Handler

Shulou Source: shulou.com Published: 2022-06-01 05:53:34 09月25日 Update

This article mainly explains "how to implement the SpringMvc exception handler". The content of the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to implement the SpringMvc exception handler.

Brief introduction of SpringMvc exception Handler

The exception information in the process of handling the request in SpringMvc is handled by the exception handler, and the custom exception handler can realize the exception handling logic of a system.

Abnormal understanding

Exceptions include compile-time exceptions and run-time exceptions, where compile-time exceptions are also called expected exceptions. Runtime exceptions are found only when the project is running, and you don't need to worry about it at compile time.

Run-time exceptions, such as null pointer exceptions and array out-of-bounds exceptions, can only be solved by programmers' rich experience and testers' continuous rigorous testing.

Compile-time exceptions, such as database exceptions, file read exceptions, custom exceptions, etc. For such an exception, you must use try catch code blocks or the throws keyword to handle the exception.

Exception handling thought

There are two types of exceptions in the system: expected exception (compile-time exception) and run-time exception RuntimeException. The former obtains exception information by capturing exceptions, while the latter mainly reduces the occurrence of run-time exceptions by means of standard code development and testing.

The dao, service and controller of the system are all thrown up through throws Exception, and finally the SpringMvc front-end controller hands it to the exception handler for exception handling, as shown below:

The global scope has only one exception handler.

First step of customizing exception classes: CustomException.javapackage com.cyb.ssm.exception;/** * customize compile-time exceptions * * @ author apple * * / public class CustomException extends Exception {private String msg; public String getMsg () {return msg;} public void setMsg (String msg) {this.msg = msg;} public CustomException (String msg) {super (); this.msg = msg }} step 2: CustomExceptionResolver.java (focus) package com.cyb.ssm.resolver;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.web.servlet.HandlerExceptionResolver;import org.springframework.web.servlet.ModelAndView;import com.cyb.ssm.exception.CustomException;public class CustomExceptionResolver implements HandlerExceptionResolver {@ Override public ModelAndView resolveException (HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {String message= "" / / exception handling logic if (ex instanceof CustomException) {message= ((CustomException) ex). GetMsg ();} else {message= "unknown error";} ModelAndView mv=new ModelAndView (); mv.setViewName ("error"); mv.addObject ("message", message); return mv Step 3: add bean to springmvc.xml step 4: jsp error page error page ${message} step 5: test class @ RequestMapping ("queryItem") public ModelAndView queryItem () throws CustomException {/ / query the database Simulate List itemList = Service.queryItemList () with static data ModelAndView mvAndView = new ModelAndView (); mvAndView.addObject ("itemList", itemList); / / set view (logical path) mvAndView.setViewName ("item/item-list"); if (true) {throw new CustomException ("I am a custom exception class");} return mvAndView;}

Thank you for reading, the above is the content of "how to implement the SpringMvc exception handler". After the study of this article, I believe you have a deeper understanding of how to implement the SpringMvc exception handler, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Tags: Processing processor compilation running testing data system logic error learning code information content only ideas situations databases pages constantly people Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Linux Shulou Technology NVidia Huawei MySQL