学无先后,达者为师

网站首页 编程语言 正文

解决springboot错误页面exception、message取值为空

作者:峰子的番茄肥牛 更新时间: 2024-03-22 编程语言

测试spring boot定制的错误页面时,发现在页面获取exceptionmessage的值为空

//自定义异常
public class MyException extends RuntimeException{
    public MyException(){
        super("抛出MyException异常");
    }
}
//Controller层代码
@RequestMapping("/errorTest")
@ResponseBody
public String errorTest(){
    throw new MyException();
}
<h1>5xx</h1><br>
<h2>页面错误能够获取的信息:</h2>
<h4>timestamp(时间戳):<span>[[${timestamp}]]</span></h4>
<h4>status(状态码): <span>[[${status}]]</span></h4>
<h4>error(错误提示):<span>[[${error}]]</span></h4>
<h4>exception(异常对象):<span>[[${exception}]]</span></h4>
<h4>message(异常信息):<span>[[${message}]]</span></h4>

 结果

解决办法:在配置文件中编写以下设置

#server错误信息要包含异常对象和异常信息(springboot2.0以上的版本需要在配置文件中指定):
server.error.include-exception=true
server.error.include-message=always

 正常显示:

 

 

 

原文链接:https://blog.csdn.net/weixin_63453779/article/details/131884362

  • 上一篇:没有了
  • 下一篇:没有了
栏目分类
最近更新