问题
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