//模拟个异常
public class UserNotExistException 
extends RuntimeException {
    
public UserNotExistException() {
        
super(
"用户名不存在!!!")
;
    }
}
//异常处理类
@ControllerAdvice
public class MyExceptionHandler {
    
//不会自动适应浏览器客户端返回的都是json
    /*@ResponseBody
    @ExceptionHandler(UserNotExistException.class)
    public  Map<String,Object> HandlerException(Exception e , HttpServletRequest request){
        Map<String,Object> map = new HashMap<>();
        map.put("code","user.notexist");
        map.put("msg",e.getMessage());
        map.put("msgd",e.getMessage());
        return map;
    }*/
//自适应浏览器和客户端
     @ExceptionHandler(UserNotExistException.
class)
    
public String 
handlerException(Exception e 
, HttpServletRequest request){
        Map<String
,Object> map = 
new HashMap<>()
;
        map.put(
"code","user.notexist")
;
        map.put(
"msg",e.getMessage())
;
       // map.put("msgd",e.getMessage());
        request.setAttribute(
"javax.servlet.error.status_code",500)
;
        request.setAttribute(
"ext",map)
;
        return "forward:/error";
    }
    //添加自己的异常属性
import org.springframework.boot.web.servlet.error.DefaultErrorAttributes
@Component
public class MyErrorAttributes 
extends DefaultErrorAttributes {
    
@Override
    public Map<String
, Object> 
getErrorAttributes(WebRequest webRequest
, boolean includeStackTrace) {
        Map<String
, Object> map = 
super.getErrorAttributes(webRequest
, includeStackTrace)
;
        map.put(
"company","atui")
;
        //我们的异常处理器携带的数据
        Map<String
,Object> ext = (Map<String
, Object>) webRequest.getAttribute(
"ext", 0)
;
        map.put(
"ext",ext)
;
        return map
;
    }
页面取值
<h1>status:[[${status}]]
</h1>
<h2>timestamp:[[${timestamp}]]
</h2>
<h2>exception:[[${exception}]]
</h2>
<h2>message:[[${message}]]
</h2>
<h2>ext:[[${ext.code}]]
</h2>
<h2>ext:[[${ext.message}]]
</h2>