1、新建一个springboot项目
添加一个全局异常的类
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler; import javax.servlet.http.HttpServletRequest; /**
* Created by Administrator on 2017/4/19 0019.
*/
@ControllerAdvice //控制器增强
public class GlobalDefaultExceptionHandler { @ExceptionHandler(value = Exception.class)//异常捕获
public void defaultErrorHandler(HttpServletRequest req, Exception e) {
e.printStackTrace();
System.out.println("GlobalDefaultExceptionHandler.defaultErrorHandler()");
}
}
Controller
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
@RequestMapping("/")
public class UserController { @RequestMapping("/testException")
public int testException(){
return 100/0;
} }
输入http://localhost:8080/testException