In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces the example analysis of SpringMVC global exception handling, the article is very detailed, has a certain reference value, interested friends must read it!
Implementation of global exception
There are three ways for Spring MVC to handle exceptions
1. Use the simple exception handler SimpleMappingExceptionResolver provided by Spring MVC
two。 Implementing the exception handling interface of Spring HandlerExceptionResolver customizes its own differences
Constant processor
3. Using @ ExceptionHandler annotations to implement exception handling
Global exception handling method 1
Configure SimpleMappingExceptionResolver object
Using SimpleMappingExceptionResolver for exception handling has the advantages of simple integration, good expansibility and no intrusiveness to the code, but this method can only obtain exception information. If there is an exception, it is not applicable to the situation that needs to obtain data other than exception.
Global exception handling method 2
Implement the HandlerExceptionResolver interface
Public class MyExceptionHandler implements HandlerExceptionResolver {
@ Override
Public ModelAndView resolveException (HttpServletRequest request
HttpServletResponse response, Object handler, Exception ex) {
Map map=new HashMap ()
Map.put ("ex", ex)
ModelAndView mv=null
If (ex instanceof ParamsException) {
Return new ModelAndView ("error_param", map)
}
If (ex instanceof BusinessException) {
Return new ModelAndView ("error_business", map)
}
Return new ModelAndView ("error", map)
}
}
Using the exception handler that implements the HandlerExceptionResolver interface to handle the exception has the advantages of simple integration, good expansibility and no invasion to the existing code. at the same time, the object that causes the exception can be obtained during the exception handling, which is helpful to provide more detailed exception handling information.
Global exception handling method 3
The page processor inherits BaseController
Public class BaseController {
@ ExceptionHandler
Public String exc (HttpServletRequest request,HttpServletResponse
Response,Exception ex) {
Request.setAttribute ("ex", ex)
If (ex instanceof ParamsException) {
Return "error_param"
}
If (ex instanceof BusinessException) {
Return "error_business"
}
Return "error"
}}
Using @ ExceptionHandler annotation to implement exception handling has the advantages of simple integration, good expansibility (only need to inherit the Controller class for exception handling to BaseController) and no additional Spring configuration, but this method is intrusive to the existing code (it needs to modify the existing code to make the relevant classes inherit from BaseController), and cannot obtain data other than exceptions during exception handling.
Handling of uncaught exceptions
For Unchecked Exception, because the code is not forced to capture, it is often ignored. If the runtime produces Unchecked Exception, and there is no corresponding capture and processing in the code, then we may have to face embarrassing 404,500... Wait for the server internal error message page. We need a comprehensive and effective exception handling mechanism. At present, most servers also support passing through the
(Websphere/Weblogic) or (Tomcat) node configures a specific exception condition
Displays the page.
The above is all the contents of the article "sample Analysis of SpringMVC Global exception handling". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
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.