错误的处理
程序启动时会默认调用
2018-09-27 15:19:43.439 |-INFO [restartedMain] org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping [543] -| Mapped "{[/error]}" onto public org.springframework.http.ResponseEntity<java.util.Map<java.lang.String, java.lang.Object>> org.springframework.boot.autoconfigure.web.BasicErrorController.error(javax.servlet.http.HttpServletRequest)
2018-09-27 15:19:43.439 |-INFO [restartedMain] org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping [543] -| Mapped "{[/error],produces=[text/html]}" onto public org.springframework.web.servlet.ModelAndView org.springframework.boot.autoconfigure.web.BasicErrorController.errorHtml(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)
添加自定义的错误页面
1 html静态页面:在resources/public/error/ 下定义
如添加404页面: resources/public/error/404.html页面,中文注意页面编码
找不到页面使用这个
2 模板引擎页面:在templates/error/下定义
如添加5xx页面: templates/error/5xx.ftl
抛异常使用这个
templates/error/ 这个的优先级比较 resources/public/error/ 高
Spring Boot 将所有的错误默认映射到/error, 实现ErrorController
第一步使用freemarker模板引擎
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.web.ErrorController;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
/**
* @task:
* @description:
* @author: sigon
* @createTime: 2018年09月27日 15:24
*/
@Controller
@RequestMapping("/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(ModelMap map) {
map.put("error", "错误信息是!!!!");
return "error/error";
}
}
第二步
在resources/templates下边创建error文件夹,在error文件夹内创建error.ftl模板文件。里边输入内容。或者一个静态页面。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>异常</title>
</head>
<body>
<h1>程序出错联系管理员</h1>
${error}
</body>
</html>
使用注解@ControllerAdvice
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;
/**
* @task:
* @description:
* @author: sigon
* @createTime: 2018年09月27日 16:10
*/
@ControllerAdvice
public class MyExceptionHandler {
private static final Logger logger = LoggerFactory.getLogger(MyExceptionHandler.class);
@ExceptionHandler({RuntimeException.class})
@ResponseStatus(HttpStatus.OK)
public ModelAndView processException(RuntimeException exception) {
logger.info("运行时异常");
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("myException", exception.getMessage());
modelAndView.setViewName("/error/500");
return modelAndView;
}
/**
* 统一异常处理
*
* @param exception
* exception
* @return
*/
@ExceptionHandler({ Exception.class })
@ResponseStatus(HttpStatus.OK)
public ModelAndView processException(Exception exception) {
logger.info("异常");
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("myException", exception.getMessage());
modelAndView.setViewName("error/500");
return modelAndView;
}
}
后续方法整理中,慢慢往里边添加。
本文介绍SpringBoot中错误处理的基本配置与自定义方法,包括使用ErrorController、自定义错误页面、异常处理注解@ControllerAdvice及@ExceptionHandler,实现更友好的错误信息展示。
6274

被折叠的 条评论
为什么被折叠?



