Quick note on Spring @ResponseStatus

Until I can find a proper explanation, this is what I see.

Spring Framework is pretty good in term of not making me reinventing things. But sometimes I wish it is a bit more consistent.

By default, any web frameworks on Earth will respond HTTP 500 on an unexpected exception/error. But Spring allows us to change the default status code by using @ResponseStatus. For example,

1
2
3
4
@ResponseStatus(HttpStatus.BAD_REQUEST)
class FooException extends RuntimeException {
    // ...
}

This works nicely as advertised, except that there is a catch here.

Suppose I want the code to respond HTTP 400 when an exception based on java.lang.AssertionError is raised.

1
2
3
4
@ResponseStatus(HttpStatus.BAD_REQUEST)
class FooException extends AssertionError {
    // ...
}

Spring Framework somehow ignores @ResponseStatus and reponds with HTTP 500 instead. Interesting, right?

I’ll dig into this when I have a free time but if you, the reader, manage to find this, please let me know on Twitter @shiroyuki.