spring boot 定制错误处理页面

  作者:记性不好的阁主

前提是有thymeleaf模板引擎的情况下


spring boot 的错误页面默认是error/路径下:


错误页面文件以 错误码.html 命名


例:404页面



一、获取错误页面各对象信息及查找错误页面优先级




获取异常信息例子:


1、获取状态码

2、获取时间戳


<main role="main" class="col-md-9 ml-sm-auto col-lg-10 pt-3 px-4">
<h1>status: [[${status}]]</h1>
<h1>timestamp: [[${timestamp}]]</h1>
</main>



二、自定义定制错误数据


1、在application.properties配置文件中配置:

#允许包含自定义错误类
server.error.include-exception=true


若不加此句则页面的exception对象不能获取


2、创建自定义异常类




package com.laoxu.springboot.exception;

public class UserNotExistException extends RuntimeException {
public UserNotExistException(){
super("用户不存在");
}
}


super中的字符串就是下面异常数据的提示信息 message


3、编写错误页面



<main role="main" class="col-md-9 ml-sm-auto col-lg-10 pt-3 px-4">
<h1>status: [[${status}]]</h1>
<h1>timestamp: [[${timestamp}]]</h1>
<h1>exception: [[${exception}]]</h1>
<h1>message: [[${message}]]</h1>
</main>


4、控制器测试


@RequestMapping("/exceptest")
@ResponseBody
public String exceptest(@RequestParam("user") String user){
if(user.equals("aaa")){
throw new UserNotExistException();
}
return "hello";
}


浏览器访问:http://localhost:8080/exceptest?user=aaa


页面显示:





三、定制错误处理器


创建异常处理器




package com.laoxu.springboot.controller;

import com.laoxu.springboot.exception.UserNotExistException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@ControllerAdvice
public class MyExceptionHandler {

@ResponseBody
@ExceptionHandler(UserNotExistException.class)
public Map<String,Object> handleException(Exception e){
Map<String,Object> map = new HashMap<>();
map.put("code","user.notexist");
map.put("message",e.getMessage());
return map;
}
}


该处理器捕获被抛出的自定义UserNotExistException异常

然后返回错误信息的json数据


访问:http://localhost:8080/exceptest?user=aaa





四、自定义错误数据及页面自适应


修改错误处理器





package com.laoxu.springboot.controller;

import com.laoxu.springboot.exception.UserNotExistException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@ControllerAdvice
public class MyExceptionHandler {

// @ResponseBody
// @ExceptionHandler(UserNotExistException.class)
// public Map<String,Object> handleException(Exception e){
// Map<String,Object> map = new HashMap<>();
// map.put("code","user.notexist");
// map.put("message",e.getMessage());
// return map;
// }


@ExceptionHandler(UserNotExistException.class)
public String handleException(Exception e, HttpServletRequest request){
//传入自定义错误状态码
request.setAttribute("javax.servlet.error.status_code",500);
Map<String,Object> map = new HashMap<>();
map.put("code","user.notexist");
map.put("message",e.getMessage());
request.setAttribute("ext",map);
return "forward:/error";
}
}


处理器将返回的map装入"ext"这个request域中


创建错误属性处理类





package com.laoxu.springboot.component;
import org.springframework.boot.web.servlet.error.DefaultErrorAttributes;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.WebRequest;

import java.util.Map;

@Component
public class MyErrorAttributes extends DefaultErrorAttributes {
//返回的map就是返回页面和json能获取到的所有字段
@Override
public Map<String, Object> getErrorAttributes(WebRequest webRequest, boolean includeStackTrace) {
Map<String, Object> map = super.getErrorAttributes(webRequest, includeStackTrace);
//额外的自定义错误信息
map.put("errorAttr","你出错啦!");
//将自定义的异常处理器中的额外的错误信息添加进来
Map<String, Object> ext = (Map<String, Object>) webRequest.getAttribute("ext", 0);
map.put("ext",ext);
return map;

}
}


1、这里新定义一个"errorAttr"错误数据属性

2、错误属性处理类将request域中取出,赋值给新的"ext",此时返回则是页面可以获取到的值


页面添加字段


<main role="main" class="col-md-9 ml-sm-auto col-lg-10 pt-3 px-4">
<h1>status: [[${status}]]</h1>
<h1>timestamp: [[${timestamp}]]</h1>
<h1>exception: [[${exception}]]</h1>
<h1>message: [[${message}]]</h1>
<h1>errorAttr: [[${errorAttr}]]</h1>
<h1>extCode: [[${ext.code}]]</h1>
<h1>extMessage: [[${ext.message}]]</h1>
</main>


访问:http://localhost:8080/exceptest?user=aaa


页面显示:





相关推荐

评论 抢沙发

表情

分类选择