110. Exception Handling Annotations - dkkahm/study-springfamework5 GitHub Wiki
Tools
- @ResponseStatus
- Annotate custom exception classes to indicate to the framework the HTTP status you want returned when that exception is thrown.
- Global to the application
- @ExceptionHandler
- Works at the controller level
- Allows you to define custom exception handling
- Can be used with @ResponseStatus for just returning a http status
- Can be used to return a specific view
- Also can take total control and work with the Model and View
- 'Model' cannot be a parameter of an ExceptionHandler method
- HandlerExceptionResolver
- Interface you can implement for custom exception handling
- Used Internally by Spring MVC
- Model is not passed
- SimpleMappingExceptionResolver
- A Spring Bean you can define to map exceptions to specific views
- You only define the exception class name (not package) and the view name
- You can optionally define a default error page
Which to Use When?
- If just setting the HTTP status
- If redirection to a view
- use SimpleMappingExceptionResolver
- If both
- consider @ExceptionHandler on the controller