From the course: Spring Web MVC 6
Unlock this course with a free trial
Join today to access over 25,500 courses taught by industry experts.
Use @ExceptionHandler - Spring Tutorial
From the course: Spring Web MVC 6
Use @ExceptionHandler
- [Instructor] Let's now go ahead and handle the other case where you're not able to find the product ID in the database. So we have tested the happy path using the getProductById method on line 29, but now we'll incorporate code to handle the exception that will arise in case the product ID is not existent in the database. For that, I'll first add a class under restcontrollers. Let's call this "ProductNotFoundException". This class let us extend runtime exception on it, and what will essentially go in this method is very simple. I will just write the constructor with the String id, this is the product ID, and I'm just going to call the super constructor with a message stating that the product ID was not found. And just append that ID to it, very simple. And I will add one more class, which is called "ProductNotFoundAdvice". Let me spell that correctly, like that. Now what is this ProductNotFoundAdvice? Generally in…