gpt4 book ai didi

java - 如何更改 ResponseStatusException 的 ErrorAttributes?

转载 作者:行者123 更新时间:2023-11-30 10:09:37 25 4
gpt4 key购买 nike

如何更改抛出 ResponseStatusException 时暴露的错误属性?

特别是我想隐藏 json 中的 exceptionerrorstatus 类型,但仅在生产期间。

    @RestController
public class MyController {
@GetMapping("/test")
public Object get() {
throw new org.springframework.web.server.ResponseStatusException(
HttpStatus.Forbidden, "some message");
}
}

结果:

{
"timestamp": "2018-11-06T12:16:50.111+0000",
"status": 403,
"error": "Forbidden",
"exception": "org.springframework.web.server.ResponseStatusException",
"message": "some message",
"path": "/test"
}

最佳答案

它的配置使用 DefaultErrorAttributes

public DefaultErrorAttributes(boolean includeException)

Create a new DefaultErrorAttributes instance.

Parameters:

includeException - whether to include the "exception" attribute

注意默认是没有

public DefaultErrorAttributes()

Create a new DefaultErrorAttributes instance that does not include the "exception" attribute.

参见 example of customizing error

关于java - 如何更改 ResponseStatusException 的 ErrorAttributes?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53171823/

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