spring - 如何在 Spring MVC 中设置错误响应状态的消息?
问题描述
我有一个这样的spring mvc处理程序:
@PostMapping("jwtToken")
fun jwtToken(@RequestBody body: JWTToken)
{
val token = body.token
if(token.isNullOrBlank())
throw ResponseStatusException(HttpStatus.UNAUTHORIZED, "Empty token")
}
如果我向它发送一个不正确的输入,这会触发异常,我会得到一个这样的响应体:
{
"timestamp": "2020-10-30T03:41:20.305+00:00",
"status": 401,
"error": "Unauthorized",
"message": "",
"path": "/auth/jwtToken"
}
当我为异常分配消息时,为什么响应中的“消息”字段为空?如何设置消息字段
解决方案
它可能与 Spring Boot 的更新行为有关。
server.error.include-message=always
in .properties 应该可以解决问题,但我更喜欢使用我自己的扩展类,如下所示:
class CustomException(message: String): Exception(message) { ... }
推荐阅读
- java - 在二叉搜索树中按顺序检索元素
- javascript - 作为 Array.prototype.every() 方法的结果,我如何返回一个布尔数组?
- r - 如何使用索引的输出来对我的数据框进行子集化?
- java - 按钮侦听器内的代码未执行
- google-search - 为什么当我输入更多字母时,我的第一个自动完成选项会消失?
- javascript - 带循环的异步和等待
- python - Pygame Snake 随机与自身碰撞
- docker - Docker 工具箱 Wildfly IP 地址 HTTP 404 响应
- android - Android Studio 重复文件
- git - 有没有办法确定我的本地克隆是否是某个 github 存储库的分支?