hey guys welcome to concept and coding this is Shan so yesterday I made a video on Spring Boot interceptors and I see lot of comments regarding filters versus interceptors and what is Filters specifically uh so I I what I anticipated from this is that there are lot of doubts between when to use filter when to use Interceptor what is the exact difference so today I will discuss uh about this and I hope that after this video your doubts will get clarified and like you can easily decide when to go for filter when to go for Interceptor okay let's start without wasting any time so let's before even seeing further right let's see this oneliner definition right and even though if you don't understand any of this terms it's okay because once we go further after that we will come back again and then we read this it will make more sense so filter it intercept the sttp request and response before they reach to the subet okay and what is Interceptor says it's specific to Spring framework and intercept HTTP request and response before they reach to the controller or I would say that before they reach to the controller and after subet okay so read it one more time it filter intercept the HTTP request and response before the request reach to the sulet and Interceptor is after sulet but before controller okay so just keep this in mind and we'll proceed further this two definition will get clear soon so if you remember this diagram which I have discussed in the first video of a spring boot discussion and in yesterday also in the uh intercept video so we know that right when a request comes in our sulet container catches it and it will pass it to one of the sulet so in Spring boot we have a dispatcher sulet dispatcher sub takes care of couple of things right which controller this request should go right and it will create an instance and it will ultimately invoke that controller okay so this is I hope it's clear but now let's see this one I have further drilled down this diagram to understand the filter and Interceptor more when the request comes in so here if you say when the request comes in it goes to our sulet container in that case let's say tomcat now when the Tomcat or sulet container get this HTTP request what it will do is before assigning to a sulet particular subet our filter comes into the picture we can have multiple filter filter one Filter 2 it's a generally they call as a chain of filters right so after filter one filter two will run if there are more filters that will run and after that one of the specific sulet will get involved right now let's say that if we are running a spring boot then dispatcher subet will get involed and and this dispatcher subet now here is our Interceptor so before this dispatcher subet handles the request to controller our interceptors comes into the picture here so with this diagram now you should uh see that where the filter lies in and where the interceptors lies in so filter lies in before the request handed over to suent and interceptors lies before the request handed over to controllers okay so now with this one few doubts might have come to your mind that hey first of all what is sulet is it possible that more than one one sulet is there let me tell you about this one so what happen is first of all what is sulet okay sulet is nothing but a Java class which accept the incoming request process it and Returns the response okay so in you can say that it is a play like a plain Java code itself it's our business logic sulet its purpose is that whatever the uh request coming in process it and return the response also okay that's what sulet is you remember that uh if you have worked in monolithic Services right let's say that we have rest apis also we have soap API also we have something called expose API for file uploading multi multiple different tasks so what we can do is we can write different suets for this so I can create one sulet which handles only rest apis I can create another subet which handles soap apis I can create another sulet which can handle only file uploads so multiple or you can say that you can create one one more suets which handle only static data like images or something okay so when the request comes to The sulet Container it decide that hey this request will be handled by which which sulet so each sulet can configure that hey this rest API let's say that this can configure the hey I can handle slash API SL star so anything which is start with/ API I can handle it okay similarly soap API can provide some configuration which sulet container will use and decide which sulet to invoke now in the microservices right and now let's say that when we are discussing about spring boot is specific and it's a microservice let's say that it's not a monolithic where I have static data also and something soap apis and all so now when you say that when we discuss more about uh microservices and specifically spring boot so there the need of multiple subet are reduced in 2017 16 when we used to code we used to have multiple subet and this is suets are nothing but a plain Java code right so whatever the controller you are writing it just like a specific apis like this is let's say/ API SL user another control / API SL let's say order but here this sulet accept anything SL API star so anything start with/ API this dispatcher subet will take care of it earlier we don't used to have this controller in this subet itself we used to have all the code logic and we have Factory here okay if it is user go and perform this logic if it is order go and perform this logic okay so what I mean to say here is that now when we are specifically doing microservices and spring boot the need of multiple servlets has been reduced so mostly our dispatcher subet if you see the default configuration is uh slash estess slash asress means anything any API dispatcher subet would be able to handle so whatever the request coming is to the tomat if tomat seees that okay request is started with Slash and anything it will just pass it to the dispatcher subet dispatcher subet will pass it to one of the controller okay so now you know what is sulet okay and this is one I show that we can create multiple sulet if we want subet one I can configur it to rest API subet 2 can be configured for soap apis similarly dispatcher subet default is slash this okay and today if even if you want hey dispatcher ofet I only want to hand SL API SL asress that also you can do okay I won't go further into that how you can do it but in the at theate configuration class itself you can do that you can update this configuration to this and only only those apis will comes to a dispatcher sub not all so with this diagram at least now we are clear that filter is before the request get assigned to a particular sub and Interceptor is after one of the subet is assigned specifically so this is very very uh spring related part so if I just uh pick a red color I would say that this is very very specific to Spring framework so Interceptor is you can say that two two points one is after the sulet is chosen and which sulet is specifically spring boot so dispatcher sub is very very specific for spring boot so Interceptor is nothing but a spring boot concept which I would say so once the spring boot sulet which is dispatcher sulet is chosen so then before this dispatcher subet assign the request to the controller intercept intercepted and for filter you can uh say that before the sulet container assign the request to one of the sulet before for that itself filters comes into the picture so now you are clear that when to use filter whenever you want to put a logic which is common for all the suets like spring boot security like spring boot security so uh spring boot security is uh uses filter because it it is applied all HTTP request and response and irrespective of of the suets because it's a very very generic code it's not very specific to our application or something so it can be applied to multiple suets also so that's why it uses a filter so it it lies in this area okay and whenever you want something specific to your application particular sulet okay I want let's say this dispatcher subet so let's say that call this as a rest API subet if anything is specifics to rest API subet then you can add Interceptor for this okay so you got it anything is specific to a particular sulet you can use Interceptor anything which is common for all the suets you can use filter okay so this is what I have explained it here so filter is used when we want to intercept HTTP request and response and logic agnostic of the underlying cetes we can have many filters and have ordering between them you can provide an ordering which one to run in which orders similarly in intercept is used when we want to intercept HTTP request and response same and add logic specific to a particular subet in this case uh Interceptor is mostly a spring boot concept you can say that so we can say that uh specific to a spring boot subet dispatcher subet so it intercept it whatever the request request is coming sulet container whatever the request is coming to dispatch a sulet let's say out of many let's assume that there are many subet so whatever it is coming to dispatch a subet this Interceptor will only take your intercept that only it can't intercept salet to a request right so that's what I mean here it's specific to a particular subid we can have many interceptors and have ordering between them two also this is also possible we can have more than one interceptors also I will tell you how you can have so here multiple interceptors and its ordering how to write it so in previous video right I already told you how you can add one Interceptor I showed you by creating one custom inter uh Interceptor and there we discussed about preh handle post handle after completion okay so here I will show you how you can add more than one Interceptor and ordering also so here if you see that one more thing is the preh handle so preh handle is something which returns true or false right this post handle returns wide after completion returns white this preh handle returns true and false so if preh handle return false next filter and controller will not get invoked so here in the code itself we can check uh so here if you see uh what was that dispatcher subet apply preh handle if preh handle return Falls this become come true and it will just simply return okay your next your next controller and next uh not next filter next controller and next Interceptor will not get invoked itself so how to create how to add more than one Interceptor so now let's say that from the previous video you already know that how to create one Interceptor similarly I have created another Interceptor and I have Auto wired here and from previous video you also know that how to register it similarly you can also add one more time registry. add Interceptor you can add the second one also and you'll see that this sequence is actually followed so since you added my custom Interceptor one my custom Interceptor 2 so this Interceptor would be executed first this would be executed second so now you have Interceptor one Interceptor 2 so when the request comes in so request goes like this and response goes like this so this is your request and this is your response so when the request comes in what it goes one Interceptor one request and then Interceptor 2 and when the response goes out so response what you will see Interceptor two will get invoked first then Interceptor one because Interceptor 2 Interceptor one like this okay so here I'm showing you the output right so only I have put a cog in preh handle post handle and after completion so here if you see when the request comes in so this is the order is maintained so Interceptor one it is going first so first it will go inside preh handle preh handle of Interceptor one so request is coming right so Interceptor one then Interceptor 2 so inter Interceptor one then preh handle of Interceptor 2 right then your code logic then your code logic now it has to response goes out right then post handle of Interceptor 2 first post handle of Interceptor one similarly after completion of two and then one and ultimately it goes out okay so this is how the sequence is maintained and how to add filter you can add your filters also like here if you see either Jakarta do suet you can use or here in the Java X do you will find filter you can choose any of this Library so I'm creating my filter one implement this filter so it has three method in it do filter and Destroy so in it like uh once the object of this my my filter is get created this method is get invoked only one time if you have to initialize something generally I've never seen we uh I have specifically initialized something but even if let's say you want to set up uh some name or something you want to specifically initialize something in this you can do that right but this is the main part do filter so do filter is like do something before you process the request so this is like do filter right next here if there are multiple filter filter 1 Filter 2 filter 3 So currently let's say this my filter one after that when you do do filter it will just call the next thing if the next part is Filter 2 it will call it if the next part is let's say code logic itself no more filter then code logic will get invoked right and the similar behavior is maintained in the filter also so when the request comes in it goes in here filter 1 Filter 2 filter 3 and the when the response goes out it goes into reverse filter 3 Filter 2 filter 1 so do filter is nothing but process the rest of the code if you are at filter one the process next of the code is Filter 2 if you are filter two next code is filter 3 if filter 3 next is your actual code logic and then reverse so this is the after processing it similarly I have added another filter my filter to imp imps filter the same thing I've just puted the logic my filter 2 inside process it something my filter 2 complete here my filter one is there okay now this is the configuration so here I am creating a bean of my filter my first filter in this I am doing its registration so this is the process how you can create an object of registration be you can set the filter so I am creating a new object because here if you see that I am not putting at theate component otherwise I don't have to create a new object so I am creating an object what is the filter this filter can take care of what all URL so I have put SL Asis right and I have set the specific order to so just specifically purposely I have given it order two for the filter one and for Filter 2 I have specifically given the order one just to show you that uh through this order we can maintain the sequence similarly I created a second filter so I have created an object of a registration Bean set the filter for this okay my filter two what all added what all for this specific URL pattern this filter will get invoke and the order I have kept it one and I have return this registration Bean that's all now when you see the output so now you have Filter 2 and then filter 1 because its order is one for the filter 2 right and this order is two I have specifically purposely mentioned this so now when the request comes in first it will invoke Filter 2 and then filter one and during the response reverse my filter 2 inside so first it goes here my inside this one then it will go what it would happen is it will go inside this my filter two inside and do do filter process rest of the code now the process rest of the codee there is more filter yes filter one so it will goes to the next filter my filter in filter one inside now this filter one it goes inside printed this do filter means process next of the code next of the code no more filter so code logic will run hitting so this is my business logic code logic now the response goes out so first it will goes out of this my filter 1 completed and then after that it will goes out of my filter 2 completed okay so this is how you can create filter and if you add both interceptors so if you add both the logic from the above one right and you run the output here if you see this would happen so your is like sulet container then filter then sulet and then controller so here you have uh this is your filter and and this will be your Interceptor so first filter will run my filter one so I have just here by doing that I just change the order properly filter one I have changed the order to one and filter two I change the order to two just so that it meant it don't confused like filter 1 Y is second so I've corrected the order here so now when the request so filter 1 Filter 2 then here we have Interceptor one Interceptor 2 so now request coming is inside filter one then it goes inside uh Filter 2 right after filter to uh there is no more filters so it will pass it to one of the sub and after the suet so this is your code logic here so after so this is your sulet now suet before it pass to the controller it will goes to the Interceptor one right so inside preh handle of Interceptor one now request goes to Interceptor 2 now ultimately your code logic your controller now the response goes out right so first Interceptor two will get invol right so this is all your Interceptor so post handle of Interceptor 2 post handle of Interceptor 1 then after completion of Interceptor 2 after completion of Interceptor one ultimately filter two and ultimately filter one and goes out so you got it like how the sequence generally maintain what's the difference between uh filter and interceptor and when to use it see if somebody say that hey you can change the header you can change the request you can change the response you can change it into Interceptor also you can change it into filter also but when to use right Interceptor also has access to request and response filter also has access to request and response but the only thing is what specific purpose you are using if it says generic no matter about suets different different suets use it at filter if it is very specific to you can say that a spring framework and very very specific to your one particular uh sulet then go for Interceptor okay I hope this clarifies but if there is any out we can discuss further and uh we'll see what question can come in we can I'll try to answer those and if you have any more additional information information about this that hey uh in this scenario filter is better than Interceptor or this I don't consider filter or Interceptor is better or not both are totally different both are totally different at different places but still if you have uh such points we can further discuss more on that okay guys thank you bye