5.错误处理
Last updated
Last updated
package com.demotm.example.controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@RequestMapping(value = "error")
public class BaseErrorController implements ErrorController {
private static final Logger logger = LoggerFactory.getLogger(BaseErrorController.class);
@Override
public String getErrorPath() {
logger.info("出错啦!进入自定义错误控制器");
return "error/error";
}
@RequestMapping
public String error() {
return getErrorPath();
}
}<!DOCTYPE html>
<html>
<head lang="en">
<title>Spring Boot Demo - FreeMarker</title>
</head>
<body>
<h1>error-系统出错,请联系后台管理员</h1>
</body>
</html><!DOCTYPE html>
<html>
<head lang="en">
<title>Spring Boot Demo - FreeMarker</title>
</head>
<body>
<h1>5xx-系统错误</h1>
<h1>${exception}</h1>
</body>
</html>@RequestMapping(value = "error")
public String error(ModelMap map) {
throw new RuntimeException("测试异常");
}/**
* 统一异常处理
*
* @param exception
* exception
* @return
*/
@ExceptionHandler({ RuntimeException.class })
@ResponseStatus(HttpStatus.OK)
public ModelAndView processException(RuntimeException exception) {
logger.info("自定义异常处理-RuntimeException");
ModelAndView m = new ModelAndView();
m.addObject("roncooException", exception.getMessage());
m.setViewName("error/500");
return m;
}
/**
* 统一异常处理
*
* @param exception
* exception
* @return
*/
@ExceptionHandler({ Exception.class })
@ResponseStatus(HttpStatus.OK)
public ModelAndView processException(Exception exception) {
logger.info("自定义异常处理-Exception");
ModelAndView m = new ModelAndView();
m.addObject("roncooException", exception.getMessage());
m.setViewName("error/500");
return m;
}package com.demotm.example.handler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.ModelAndView;
/**
* 异常处理类
*
* @version 1.0
*/
@ControllerAdvice
public class ErrorExceptionHandler {
private static final Logger logger = LoggerFactory.getLogger(ErrorExceptionHandler.class);
/**
* 统一异常处理
*
* @param exception
* exception
* @return
*/
@ExceptionHandler({ RuntimeException.class })
@ResponseStatus(HttpStatus.OK)
public ModelAndView processException(RuntimeException exception) {
logger.info("自定义异常处理-RuntimeException");
ModelAndView m = new ModelAndView();
m.addObject("roncooException", exception.getMessage());
m.setViewName("error/500");
return m;
}
/**
* 统一异常处理
*
* @param exception
* exception
* @return
*/
@ExceptionHandler({ Exception.class })
@ResponseStatus(HttpStatus.OK)
public ModelAndView processException(Exception exception) {
logger.info("自定义异常处理-Exception");
ModelAndView m = new ModelAndView();
m.addObject("roncooException", exception.getMessage());
m.setViewName("error/500");
return m;
}
}<!DOCTYPE html>
<html>
<head lang="en">
<title>Spring Boot Demo - FreeMarker</title>
</head>
<body>
<h1>500-系统错误</h1>
<h1>${exception}</h1>
</body>
</html>@RestControllerAdvice
public class ExceptionHandlerController {
@ExceptionHandler(RuntimeException.class)
@ResponseStatus(HttpStatus.OK)
public Map<String, Object> handleException(RuntimeException exception) {
Map<String, Object> result = new HashMap<>();
result.put("result", "fail");
result.put("errMsg", exception.getMessage());
return result;
}
}