Exception Handing via @ExceptionHandler and @ControllerAdvice

Coding Saint
A free video tutorial from Coding Saint
Technical Architect
4.2 instructor rating • 2 courses • 16,389 students

Learn more from the full course

Spring Boot REST API and Microservices - Spring Cloud

Simple Short Tutorial for Spring Boot and Microservices with Spring Cloud

05:46:49 of on-demand video • Updated March 2020

  • You will start coding in Spring Boot and will learn Spring and Microservices in detail
  • It will increase you to understand detailed working and annotations of Spring
  • You will learn to perform CREATE,UPDATE,RETRIEVE and DELETE (CRUD) operations on database layer
  • You will learn advance REST technology which will make you aware of HTTP verbs and RESTful Maturity via Richandson maturity level
  • You will learn fine details of Error and Exception Handling of REST API
  • Additionally you will be aware of Random numbers and values generation via Spring
  • You will get knowledge on Profiles and externalising configurations a way to handle application in different environment as DEV,QA ,PROD
  • You will learn reason to use Microservices and also when not use Microservices
  • Learn how to code in Feign Declarative REST client and reduce Boilerplate code of Spring REST Template code
  • You will be able to answer needs of load balancing via Ribbon Client Side load balancing
  • You will understand how new service in Microservices are discovered via Service Discovery
  • You will learn how to manage configurations in Microservices using Config Server
  • You will learn intricacy of logging and Tracing logs in distributed system using Sleuth and Zipkins
  • Build Resilient Systems with Hystrix Fault Tolerance
  • Learn how to automatically update configuration without server restart using Cloud Bus
English [Auto] Hi. So no once we have created our custom Exception like user not found exception and we still want to do some modification in the way we are getting the response in case of exception we will use a few more annotations and we will explore the functionality of custom Exception Handling so in order to do that. Let us then go back to our source code. So we already have created a package exception so at first we will try to create an entity which will define the real wide exception we look for let us say we are creating a class call application exception of the applications errors and in this particular class we will have fields which will define how our application errors will look. So how about adding a message then about adding the fact exact a date sad or code okay. So not these are the three fields we are trying to add here. We will just create a getter instead of for it quickly. Now we have a class which says how our application error should look. This is a very basic and simple class. You can actually create a lot more complex and better classes. If your application requires but for this to move we are using very simple class B which is application error which contains just the message which is required to send and if you are providing any code which depends on you. And at the end a date. Okay so now let's create a simple constructor so we will be creating only constructor only with this message and date method and code and list the date be added ones we are trying to handle it but this all depends on the way you want to create a constructor even so I'm just removing this data from here. This we will be adding the date we will try to add once we are trying to create. I mean once for trying to handle this exception. Okay so now this entire entity is really which is called application error and it should have these three fields. So how to handle it now. So the key is currently that we are using it like user not found exception and how we will handle user not for an exception. So we'll just create a more one more class here. We'll call it application. Exception handler No. This application exception handler is there. So what it is supposed to do it is supposed to handle our exception this spring already handles out of exceptions. Now we want to override it. So let's see how we can do that. We will just extend this to response entity exception handler. So currently whatever response we are getting it is being handled by spring. We are just telling that we have a new handler. So hey spring you'll just handler in some cases so we'll also have would say that in which case is that the first thing that we would like to tell that this is an advice that we are giving for controller exception so I'm just handling controller advice here. And after that we would just have to add one more feed which is a direct exception handler Okay so we'll now create a method. So now we want to send a response entity of application error so we'll just write your application area and handle use. Not found exception. So Hoggard had known that we have to handle using an phone exception. We'll also have to pass that as a part of me to. And there is one more parameter which we need to pass which is verbal request since we are dealing with VIPs. So with this particular request we can get few more information. Whatever the input is coming the number that we have not sent any particular kind of exceptions back. So we are not currently visible from here. We are not sending anything back so that's why. Oh there is no return the statement that says we are not sending any anything. So no one's because we are not sending anything. We are getting this error let us add code here and see how does it work. Okay so we have a message. We have a code for Will and it say that we have a message like whatever we are getting it from this particular exception we learned that get the message and let's and a code let's see say because it is for you for we are still adding further for here and you can define your own code if you wish. And we also need to add a time. So just don't. You could have also added or did here or only if you would have if you would have created such a constructor and just return it return new response entity which does have this particular error okay. Um. So we have added that and let s and the code is due to be a status like not font okay. No it is done. So if you see this come to control our advice is handling. One exception handler that we have provided for our own exception. But you can actually have a lot of different kind of handlers with the great exception handler. So all you need to do is to create different methods here which will handle it or a particular kind of exception loops. There is one area that I have currently just seen. It is a service not fun but we are handling you use not found right the user not found. Exception. Okay so now it should work. So we will just test it. And let's just go back quickly we are going back to our Controller. So just to see that everything is fine. So in this case we are sending this but will use it not for an exception. And what we are trying to do we have a handler to it which is a controller advice which particularly handles user not for an exception and sends a response in application error format. Wow. So now if you see that we are getting their response back in the VA we want it. So we have a message that date and also the code available. So similarly you can design your own exception handler for different kind of exceptions and even the exception class.