In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about how to configure system global exception mapping handling in SpringBoot2. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
I. Classification of anomalies
From the point of view of the system handling exceptions, the exception classification here is mainly divided into two categories: business exception and system exception.
1. Business exception
Business exception is mainly some predictability exception, dealing with business exception, used to prompt the user's operation and improve the maneuverability of the system.
Common business exception tips:
1) Please enter xxx
2) xxx cannot be empty
3) duplicate xxx, please replace
2. System exception
System exceptions are mainly unpredictable exceptions. Dealing with system exceptions can show a friendly user interface, which is not easy to cause disgust to users. If it is a financial system, a system abnormal crash interface appears in the user interface, which is likely to directly lead to the loss of users.
Common system exception prompts:
1) Page loss 404
2) Server exception 500
Second, solve the 404 interface after the launch of the application
1. Introduce the page Jar package org.springframework.boot spring-boot-starter-thymeleaf2, customize the home page interface import org.springframework.stereotype.Controller;import org.springframework.ui.ModelMap;import org.springframework.web.bind.annotation.RequestMapping;@Controllerpublic class IndexController {@ RequestMapping ("/") public String index (ModelMap modelMap) {modelMap.addAttribute ("name", "smile"); return "index" 3. Home page interface 4, running effect
Exception handling in SpringBoot2.0 1. Project structure diagram
2. Custom business exception class public class ServiceException extends Exception {public ServiceException (String msg) {super (msg);}} 3. Custom exception description object public class ReturnException {/ / response code private Integer code; / / exception description Url private String url; / / omit get set method} 4. Unified exception handling format
1) two basic notes
@ ControllerAdvice defines a unified exception handling class
@ ExceptionHandler defines how to handle exception types
2) Code implementation
Import org.springframework.web.bind.annotation.ControllerAdvice;import org.springframework.web.bind.annotation.ExceptionHandler;import org.springframework.web.bind.annotation.ResponseBody;import org.springframework.web.servlet.ModelAndView;import javax.servlet.http.HttpServletRequest @ ControllerAdvice// exception returns the same as ExceptionHandler + ResponseBody comment / / @ RestControllerAdvicepublic class HandlerException {/ * Custom Business exception Mapping in Json format, and returns JSON format prompt * / @ ExceptionHandler (value = ServiceException.class) @ ResponseBody public ReturnException handler01 (HttpServletRequest request,ServiceException e) {ReturnException returnException = new ReturnException (); returnException.setCode (600); returnException.setMsg (e.getMessage ()) ReturnException.setUrl (String.valueOf (request.getRequestURL ()); return returnException;} / * * Service exception * / @ ExceptionHandler (value = Exception.class) public ModelAndView handler02 (HttpServletRequest request,Exception e) {ModelAndView modelAndView = new ModelAndView (); modelAndView.addObject ("ExeMsg", e.getMessage ()); modelAndView.addObject ("ReqUrl", request.getRequestURL ()) ModelAndView.setViewName ("/ exemsg"); return modelAndView }} 5. Simple test interface @ Controllerpublic class ExeController {/ * * {* "code": 600,* "msg": "Business exception: ID cannot be empty" * "url": "http://localhost:8003/exception01" *} * / @ RequestMapping (" / exception01 ") public String exception01 () throws ServiceException {throw new ServiceException (" Business exception: ID cannot be empty ") } @ RequestMapping ("/ exception02") public String exception02 () throws Exception {throw new Exception ("abnormal, all down");}}
Thank you for reading! This is the end of the article on "how to configure system global exception mapping handling in SpringBoot2". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.