How to change ErrorAttributes of ResponseStatusException?

六眼飞鱼酱① 提交于 2020-01-23 10:09:28

问题


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

回答1:


It's configure using DefaultErrorAttributes

public DefaultErrorAttributes(boolean includeException)

Create a new DefaultErrorAttributes instance.

Parameters:

includeException - whether to include the "exception" attribute

Notice the default is without

public DefaultErrorAttributes()

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

See example of customizing error



来源:https://stackoverflow.com/questions/53171823/how-to-change-errorattributes-of-responsestatusexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!