6

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"
}
membersound
  • 81,582
  • 193
  • 585
  • 1,120

1 Answers1

4

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

Community
  • 1
  • 1
Ori Marko
  • 56,308
  • 23
  • 131
  • 233
  • So i would override `DefaultErrorAttributes` and `errorAttributes.remove("path")` etc? – membersound Nov 06 '18 at 12:32
  • @membersound Or call `Map errorAttributes = super.getErrorAttributes(requestAttributes, false); ` – Ori Marko Nov 06 '18 at 12:35
  • 1
    @membersound IMO this answer clearly solve the problem: make an implementation of DefaultErrorAttributes for each profile. –  Nov 06 '18 at 12:43