本文介绍了如何更改 ResponseStatusException 的 ErrorAttributes?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何更改抛出 ResponseStatusException
时暴露的错误属性?
特别是我想隐藏 json 中的 exception
、error
和 status
类型,但仅在生产期间.
@RestController公共类 MyController {@GetMapping("/测试")公共对象获取(){抛出新的 org.springframework.web.server.ResponseStatusException(HttpStatus.Forbidden, "一些消息");}}
结果:
{"时间戳": "2018-11-06T12:16:50.111+0000",状态":403,"错误": "禁止",异常":org.springframework.web.server.ResponseStatusException","message": "一些消息",路径":/测试"}
解决方案
public DefaultErrorAttributes(boolean includeException)
创建一个新的 DefaultErrorAttributes
实例.
参数:
includeException - 是否包含异常";属性
注意默认是没有
public DefaultErrorAttributes()
创建一个新的 DefaultErrorAttributes
实例,该实例不包含异常"属性.
参见自定义示例错误
How can I change the error attributes that are exposed when throwing a ResponseStatusException
?
Especially I want to hide the exception
, error
and status
type in the json, but only during production.
@RestController
public class MyController {
@GetMapping("/test")
public Object get() {
throw new org.springframework.web.server.ResponseStatusException(
HttpStatus.Forbidden, "some message");
}
}
Result:
{
"timestamp": "2018-11-06T12:16:50.111+0000",
"status": 403,
"error": "Forbidden",
"exception": "org.springframework.web.server.ResponseStatusException",
"message": "some message",
"path": "/test"
}
解决方案
It's configure using DefaultErrorAttributes
public DefaultErrorAttributes(boolean includeException)
Notice the default is without
public DefaultErrorAttributes()
See example of customizing error
这篇关于如何更改 ResponseStatusException 的 ErrorAttributes?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!