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:
- The @ Autowired annotation in springboot is invalid in ordinary classes. How to solve and use the null pointer exception java.lang.nullpointerexception
- Exception handling of httpmessage notwritableexception in springboot
- How to handle when select single is not allowed in loop
- Default constructor cannot handle exception type FileNotFoundException thrown by implicit super cons
- How to realize automatic assembly in springboot
- How to handle error 2755 when installing software under windows
- How to Fix error performing isolated work; SQL [n/a]; nested exception is org.hibernate.exception.SQLGrammarE
- #How to handle the installation rollback and 1205 connection error of pulse secure ා
- Springboot startup error – classnotfound Exception:EnhancerBySpringCGLIB
- Springboot project startup exception – required a single bean, but 2 were found
- Springboot startup exception: error creating bean with name ‘permissioncontroller’
- How to Fix Exception in thread “main“ com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications
- The solution of a Java exception has occurred. And error exception in thread when eclipse runs
- Realize the simplest recursive call, simulate exception in thread “main” java.lang.stackoverflowerror exception
- Net Q & A: how to avoid the exception thrown by max() on emptyenumerable?
- How to solve the problem of artifact contains illegal characters when creating springboot project with idea
- Springboot error, unable to read configuration file: could not resolve placeholder ‘xxx’ in value “${XXX}
- You may need an appropriate loader to handle this file type, currently no loaders are configured to
- Springboot uses druid to log in MySQL. An error occurred: access denied: errorcode 1045, state 28000
- Module parse failed:Unexpectedtoken (1:0)You may need an appropriate loader to handle this file type