Definition of a global process class, and a method of handling abnormal
/ **
* global exception handler for Controller layer, service can capture abnormal
* Ajax request returns data json;
* @author: the lazy
* @date: 2019/8/6 16:09
* @Version 1.0.0
* @description
* /
@ControllerAdvice
public class AppControllerAdvice {
Private static Logger Logger = LoggerFactory.getLogger (AppControllerAdvice.class);
/ **
* returns json data type for @ResponseBody, if returned to view, without @ResponseBody
* @param EX
* @return
* /
@ExceptionHandler (value = Exception.class)
@ResponseBody
public the Map <String, Object> from exceptionHandler (exception EX) {
the Map <String, Object> = new new Map the HashMap <> ();
// console printing abnormal
ex.printStackTrace ();
logger.error ( "exception information:" + ex.getMessage ());
map.put ( "Status", 500);
map.put ( "the Message", "Out of Service");
return the Map ;
}
}
Control class shows exception handling
/ **
* springboot use global exception, will capture the exception, but does not perform the back of the exception clause, that
* after the exception occurs, jump directly to the processing method of the Exception class, after the implementation of the results return;
* If you use try ... catch .. statement block, you can catch the exception, and can execute the statement following the normal catch;
* that is, where will jump to the index.html page;
* @author: lazy
* @date: 2019/11/27 9:23
* @Version 1.0.0
* @Description
* /
@Controller
public class IndexAction {
@RequestMapping ( "/ index")
public String index () {
String STR = null;
str.replaceAll ( ",", "");
/ * the try {
String STR = null;
str.replaceAll ( ",", "");
} the catch (Exception E) {
e.printStackTrace ();
}*/
// Use the global exception handling, an exception occurs if the above is not performed, may be performed using try..catch
System.out.println ( "exception occurs");
return "index";
}
}