SpringBoot exception handling in five ways: a, a custom error page:
SpringBoot default exception handling mechanism need to be in SRC/main/resources/templates directory to create the error. The HTML page (no matter what exceptions will intercept). Different to intercept errors is necessary under the templates to create the error folder named with error code to the HTML
2: @ ControllerAdvice + @ ExceptionHandler annotation processing abnormal
you need to create a global exception class that can handle the exception. You need to add the @ControllerAdvice annotation on this class.
@ ControllerAdvice
public class GlobalException {
@ ExceptionHandler (value = {Java. Lang. ArithmeticException. Class})
public ModelAndView arithmeticExceptionHandler (Exception e) {
ModelAndView mv = new ModelAndView();
mv. AddObject (” error “, e. oString ());
mv. SetViewName (” error “);
return mv;
}
}
> All you need to do is add the following method to the controller:
@ExceptionHandler(value={java.lang.ArithmeticException.class})
public ModelAndView arithmeticExceptionHandler(Exception e) {
ModelAndView mv = new ModelAndView();
mv. AddObject (” error “, e. oString ());
mv. SetViewName (” error “);
return mv;
}
requires the implementation of the HandlerExceptionResolver interface in the global exception class
@Configuration
public class GlobalException implements HandlerExceptionResolver {
@Override
public ModelAndView resolverException(HttpServletRequest request, HttpServletResponse response, Object object, Exception exception) {
ModelAndView mv = new ModelAndView();
if(Exception instanceof ArithMeticException) {
.setViewName (” error “);
}
mv. AddObject (” error “, the exception. The toString ());
return mv;
}}
5, configuration, exception handling SimpleMappingExceptionResolver
in the global exception class to add a method to complete the unification of the exception class
@Configuration
public class GlobalException {
public SimpleMappingExceptionResolver getSimpleMappingExceptionResolver() {
SimpleMappingExceptionResolver resolver = new SimpleMappingExceptionResolver();
Properties properties = new Properties();
properties. The put (” Java. Lang. ArithmeticException “, “error”);
resolver.setExceptionMappings(properties);
return resolver;
}
}
Read More:
- Java gets the type t.class of generic t
- java.lang.UnsupportedOperationException resolvent
- Resolve rabbitmq declarationexception: failed to declare queue exception
- Springboot + mybatis plus transaction management
- Failed to configure a DataSource: ‘url’ attribute is not specified and no embedded (How to Fix)
- Using JSON to report errors
- Elasticsearch in Spring uses Spel to dynamically create Index of Documet class
- stay web.xml Configure error page in
- Springboot integrates quartz timing tasks
- Some problems encountered by Android Aidl
- How to Fix Invalid bound statement (not found) Error
- Springboot integration redis reports non null key required (solved)
- Solution of idea using @ Autowired annotation to report errors
- Realization of springboot authorization verification technology based on JWT
- Could not find or load main class
- Problems encountered after Maven installation or upgrade: could not find or load main class org.codehaus.plexus .class…..
- Failed to execute goal org.apache.maven.plugins:maven-javadoc-plugin:2.9.1:jar (default) on project
- The problem of mobile hybrid development RN Android deployment appears Unsupported class file major version xx
- Springboot integrated with mybatis
- Related configuration of mybatis project