spring-boot - 关于 ResponseStatusException 返回错误对象中的空消息字段
问题描述
我在 Spring Boot 中有以下 API 端点:
@GetMapping("/metrics/{id}")
public MetricItem helperMethod(@PathVariable String id) {
try {
return customMetricRepository.find(id);
} catch (IllegalArgumentException | NullPointerException | MetricDoestNotExistException e) {
if (e instanceof MetricDoestNotExistException) {
throw new ResponseStatusException(
HttpStatus.NOT_FOUND, "Metric does not exist", e);
}
else {
throw new ResponseStatusException(
HttpStatus.BAD_REQUEST, ErrorCodes.UUID_NOT_FOUND, e);
}
}
}
当找不到指标时,我想在对象的错误字段的消息字段中看到我的“指标不存在”,但我得到以下信息:
{
"timestamp": "2020-07-01T22:53:44.494+00:00",
"status": 404,
"error": "Not Found",
"message": "",
"path": "/api/metrics/e1571021-6a17-4b02-b238-72ac3a1c26cd"
}
解决方案
we just had the same issue.
I found a github discussion where someone pointed out that there was a change within spring-boot 2.3.
https://github.com/spring-projects/spring-boot/issues/22049
If you look into the release notes you can find this section:
https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.3-Release-Notes#changes-to-the-default-error-pages-content
There it says:
Changes to the Default Error Page’s Content
The error message and any binding errors are no longer included in the default error page by default. This reduces the risk of leaking information to a client. server.error.include-message and server.error.include-binding-errors can be used to control the inclusion of the message and binding errors respectively. Supported values are always, on-param, and never.
推荐阅读
- javascript - 我正在尝试使用多个组件重新制作 Vuex 的入门项目,但无法弄清楚如何从组件调用根方法
- kubernetes - 在 Kubernetes 上正确设置 Datadog 日志摄取
- c++ - 如何在不强制转换每个参数的情况下删除初始化列表中从 int 到 char 的缩小转换?
- algorithm - 如何处理通过 Dijkstra 算法遍历的图中的“组合节点”?
- android - 如果我销售的实物商品也解锁了对我们应用程序的访问权限,我是否必须使用谷歌应用内购买?
- javascript - JavaScript 计算器中只有 1 位小数?
- python - pygame窗口永远加载
- c++ - GetTokenInformation 在非提升时不返回所有权限的状态
- java - onKeyDown 防止硬件音量按钮工作
- vb.net - 我的应用程序在使用 TNS 时遇到了这个奇怪的错误,但是