简体   繁体   中英

How to change ErrorAttributes of ResponseStatusException?

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM