具體代碼以下:java
package com.cori.config; import javax.servlet.http.HttpServletRequest; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseBody; /** * * 一、新建一個類GlobalDefaultExceptionHandler * 二、在class註解上@ControllerAdvice * 三、在方法上註解上@ExceptionHandler(value = Exception.class),用於攔截相應的信息 * 四、若是返回view,方法的返回值是ModelAndView * 五、若是返回的是String或者是JSON,那麼在返回的方法上添加@ResponseBody註解 * */ @ControllerAdvice public class GlobalDefaultExceptionHandler { @ExceptionHandler(value = Exception.class) @ResponseBody public String defaultErrorHandler(HttpServletRequest req,Exception e) { return "服務器繁忙,請稍後再試..."; } }
Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback. Tue Jun 05 23:07:14 CST 2018 There was an unexpected error (type=Internal Server Error, status=500). Incorrect result size: expected 1, actual 2
服務器繁忙,請稍後再試...