具体代码以下: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
服务器繁忙,请稍后再试...