Exception Handling
Exception handling is one of the important parts of developing web services. When something goes wrong, we would want to return a good description of what went wrong to the service consumer. You would not want the service to crash without returning anything useful to the service consumer.
Spring Boot provides good default exception handling. We will start with looking at the default exception handling features provided by Spring Boot before moving on to customizing them.
Spring Boot Default Exception Handling
To understand the default exception handling provided by Spring Boot, let's start with firing a request to a nonexistent URL.
Non-Existent Resource
Let's send a GET
request to http://localhost:8080/non-existing-resource
using a header (Content-Type:application/json
).
The following screenshot shows the response when we execute the request:
The response is as shown in the following code snippet:
{ "timestamp": 1484027734491, "status...