springboot controller 中抛出异常会交给默认 /error来进行处理
我们可以将/error 映射到一个特定的controller 替代默认实现
1.重写 /error
@Controller
public class ErrorController extends AbstractErrorController{
public ErrorController(ErrorAttributes errorAttributes) {
super(new DefaultErrorAttributes());
}
@Override
public String getErrorPath() {
return null;
}
private static final String ERROR_PATH = "/error";
private static final int OK = 200;
private static final String ERROR_MESSAGE = "系统内部错误,请联系管理员!";
@RequestMapping(ERROR_PATH)
public ModelAndView ExceptionHandler(HttpServletRequest request,HttpServletResponse response){
// 返回成功状态
response.setStatus(OK);
Map model = Collections.unmodifiableMap(getErrorAttributes(request, false));
int status = (Integer)model.get("status");
String message = (String)model.get("message");
// 判断请求类型
String header = request.getHeader("X-Requested-With");
boolean isAjax = "XMLHttpRequest".equalsIgnoreCase(header);
switch (status) {
case 404:
return new ModelAndView("error/404");
case 500:
if(isAjax){
Map error = new HashMap<>();
error.put("code", 1);
error.put("message", ERROR_MESSAGE);
writeJson(response,error);
return null;
}else {
return new ModelAndView("error/500","message",ERROR_MESSAGE);
}
default:
if(isAjax){
Map error = new HashMap<>();
error.put("code", 1);
error.put("message", message);
writeJson(response,error);
return null;
}else {
return new ModelAndView("error/error","message",message);
}
}
}
protected void writeJson(HttpServletResponse response,Map error){
try {
response.setContentType("text/html;charset=UTF-8");
response.getWriter().write(JSON.toJSONString(error));
} catch (IOException e) {
e.printStackTrace();
}
}
}
2.springmvc 统一异常处理
@ControllerAdvice
public class GlobalExceptionHandler {
private static final String ERROR_MESSAGE = "系统内部错误,请联系管理员!";
@ExceptionHandler(value = Exception.class)
public ModelAndView ExceptionHandler(HttpServletRequest request,HttpServletResponse response, Exception e) {
String header = request.getHeader("X-Requested-With");
if("XMLHttpRequest".equalsIgnoreCase(header)){
Map error = new HashMap<>();
error.put("code", 1);
error.put("message", ERROR_MESSAGE);
writeJson(response,error);
return null;
}else {
ModelAndView mv = new ModelAndView("error/500");
mv.addObject("message", ERROR_MESSAGE);
return mv;
}
}
protected void writeJson(HttpServletResponse response,Map error){
try {
response.setContentType("text/html;charset=UTF-8");
response.getWriter().write(JSON.toJSONString(error));
} catch (IOException e) {
e.printStackTrace();
}
}
}
404.sos{width: 500px;height: 200px;margin: 300px auto 0px auto;font-family:"微软雅黑 Light", "微软雅黑";}
.sos .nofind{ font-size:18px; color:#666; text-align:center; margin-top:30px;}
.sos .imgdiv{text-align: center;}
.sos{ width:652px; height:92px; position: absolute; top:50%; left:50%; margin-left:-326px; margin-top:-230px; font-family:"微软雅黑 Light", "微软雅黑";}
.sos .errormessage{ font-size:18px; color:#666; text-align:center; margin-top:30px;}
#base.html
测试异常处理.main{
width: 500px;
height: 500px;
margin: 30px auto;
border: 1px black solid;
}
测试系统内部异常,前端显示问题!
1.ajax请求失败
2.网页请求失败
function ajax(){
$.post("/test/json",{},function(json,status){
if(status == "success"){
alert(JSON.stringify(json));
}
},"json");
}
function page(){
window.location.href = "/test/page"
}
@Controller
public class TestController{
@RequestMapping("test/base")
public ModelAndView basePage() {
return new ModelAndView("base");
}
@RequestMapping("test/page")
public ModelAndView testPage() {
System.out.println(1/0);
return new ModelAndView("test1");
}
@RequestMapping("test/json")
public ResultHelper testJson() {
System.out.println(1/0);
return ResultHelper.success();
}
}
如果需要打印一些异常信息,完全可以通过request中进行获取。