📜Introduction
In the previous article, we learned how to return the proper response status of CREATED when we created the resource. In this article, we will learn what the response should be when a user resource does not exist.
Let's see the steps to execute a simple response.
📚Steps to Execute a Simple Response
👉Open Postman and select the GET method.
👉Click on history and select GET request.
👉Enter the URL with a user id. The user id won't exist.
👉Click on the send button.
Even though the resource is non-existent, we receive the status 200 OK and empty body, which is a successful response. If a resource is unavailable, it is not the appropriate response.
Now let's see how to fix the above issue.
👉Open the user resource file.
👉Create a UserNotFoundException class. It is a checked exception.
@GetMapping("/users/{id}")
public User retriveUser(@PathVariable int id)
{
User user= service.findOne(id);
if(user==NULL)
{
throw new UserNotFoundException("id: "+ id);
}
return user;
}
👉Now, create constructors from Superclass.
Right-click on the file -> Source -> Generate Constructors from Superclass... -> check the RuntimeException(String) -> Generate.
👉Now, our UserNotFoundExcepion.java file will look something like this:
package com.javatpoint.server.main.user;
public class UserNotFoundException extends RuntimeException
{
public UserNotFoundException(String message)
{
super(message);
}
}
👉Open the Postman and generate a Get response. It shows the Status: 500 Internal Server Error.
However, the 500 Internal Server Error status code is not the proper response for a resource that was not found. So, to produce the Status: 404 Not Found, we will add the annotation @ResponseStatus.
package com.javatpoint.server.main.user;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;
@ResponseStatus(HttpStatus.NOT_FOUND)
public class UserNotFoundException extends RuntimeException
{
public UserNotFoundException(String message)
{
super(message);
}
}
👉Again, do the same, open the Postman and generate a Get response.
We receive a suitable response. If a user resource is missing, the status will be 404 Not Found. We received this return status because the default error handling gave the content of the request.
Error management is provided by the Spring Web MVC framework and Spring Boot. Spring Boot automatically configures some default exception handling. It is crucial to have an exception message consistently received for all of the services inside our enterprise.
Frequently Asked Questions
How should a spring boot exception be handled?
An annotation called @ExceptionHandler handles particular exceptions and offers the client custom responses. Create an extension of the RuntimeException class. To handle exceptions, you can define the @ExceptionHandler method as illustrated.
What does "404 error" mean exactly?
The communication office staff would respond to a request with "Room 404: file not found" if the desired document couldn't be found. Although the error message still refers to Room 404, it was allegedly later condensed when the system was automated.
How does Spring Boot handle checked exceptions?
The important thing is to throw RuntimeExceptions after catching checked exceptions in the program. Allow the Controller class to throw these exceptions away before applying the ControllerAdvice.
Can a post return 404?
No. Your API should be "user-friendly," which means that if there is an error, it should return the information to allow the user to identify the issue. A 404 response is equivalent to stating that the service could not be found, which is untrue.
When should you use 404?
The status code 404 (Not Found) should be used in its place if the server does not know or cannot determine if the condition is permanent. Unless otherwise stated, this response is cacheable.
Conclusion
This article taught us about exception handling and how to implement the same with a walkthrough of the steps. That's the end of the article. I hope you all like this article.
If you want to learn more, check out our articles on Construct the Full K-ary Tree from its Preorder Traversal, Regular and Bipartite graphs, What Is Web2Py?, Why To Use Web2py?, Postbacks and Internationalization in web2py, Third Party Modules In Web2py, Tasks In Web2py, and XML in Web2py.
Happy Coding!




