Status codes and exception handling

The first thing we want to do is to correctly handle response statuses. By default, Spring automatically deals with some statuses:

  • 500 Server Error: This indicates that an exception occurred while handling the request.
  • 405 Method not Supported: This comes up when you use an incorrect method on an existing handler.
  • 404 Not Found: This comes up when the handler does not exist.
  • 400 Bad Request: This indicates that the request body or parameter does not match the server's expectation.
  • 200 OK: It is thrown for any request handled without an error.

With Spring MVC, there are two ways to return status codes:

  • Returning a ResponseEntity class from a REST controller
  • Throwing an exception that will be caught in dedicated handlers ...

Get Spring MVC: Designing Real-World Web Applications now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.