gpt4 book ai didi

spring-boot - 关于 ResponseStatusException 返回错误对象中的空消息字段

转载 作者:行者123 更新时间:2023-12-03 08:39:50 24 4
gpt4 key购买 nike

我在 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"
}

最佳答案

我们只是遇到了同样的问题。
我发现了一个 github 讨论,其中有人指出 spring-boot 2.3 中发生了变化。
https://github.com/spring-projects/spring-boot/issues/22049
如果您查看发行说明,您可以找到此部分:
https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.3-Release-Notes#changes-to-the-default-error-pages-content
那里说:
更改默认错误页面的内容
默认情况下,错误消息和任何绑定(bind)错误不再包含在默认错误页面中。这降低了向客户泄露信息的风险。 server.error.include-message 和 server.error.include-binding-errors 可用于分别控制消息的包含和绑定(bind)错误。支持的值为 总是 , 参数 , 和 从不 .

关于spring-boot - 关于 ResponseStatusException 返回错误对象中的空消息字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62686567/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com