Transcript for:
Spring Boot Interview QA Series Part 4

hi everyone welcome to Java tii today we'll dive into part four of our in-depth spring boot interview QA series but before we get started if you have not checked out previous videos then be sure to do that for a solid foundation okay so without any further delay let's get started [Music] so let's begin with our first question that is have we worked on restful web services if yes what all HTTP methods have you used in your project so there are different type of HTTP method like post put get patch and delete and each HTTP method serve a specific purpose in web development for example if you want to create any resource on the server you can use this post HTTP method you can use the put if you want to update something on the resource if you want to retrive the information from the resource you can use this get HTTP method similarly if you want to update partially then you can use patch HTTP method and if you want to remove the resource from the server then you can use the delete HTTP method so there are many more HTTP methods available in the web development but these five are hugely used in industry okay post put get patch and delete for different operation now let's move to the next question that is how can you specify the HTTP method type for your rest endpoint so before specifying the HTTP method like post put get patch and delete first you should know what action you want to perform in your endpoint whether you want to create something or you want to update something or you want to remove or you want to retrive First you should know the action what you want to perform based on that you can Define The annotation given by the spring framework so let me work you through different annotation for different HTTP method let's go to the ID and let me go through any of the controller class then simply I'll just write a method now once you define the method next you need to identify what action you are going to perform whether you want to retrive something from the server or you want to update or if you want to insert based on that you need to choose the correct HTTP method specific annotation for example if I want to retrive something I can Define G mapping okay and if You observe this annotation itself self-explanatory that what action you are going to use in this method okay get means retrive similarly if you want to perform something insert operation kind of thing or if you want to insert something to the server you can use post mapping fine similarly for update you can use put mapping for partial update you can use patch mapping and for removal the resource from the server you can use delete mapping fine and in case of this post and put makes you to define the request body let's say I want to pass some object which is nothing my input I need to Define at theate request body here fine in case of get and delete mapping you can Define the input as part of URL itself either using path variable or using request parameter I will come to that point okay so get and delete you must need to pass the input as part of the request URL but in case of post put you can pass the object as a request body along with that if you want to pass the input as part of request URL you can do that okay there is no convention to follow but in case of post and put if you want you can pass the request as part of the URL itself but gate mapping you must need to pass the input as part of the request URL okay so this is how you can Define http method specific annotation and if You observe each and every annotation extends from request mapping okay so this request mapping is the parent annotation for all the HTTP method specific annotation because in earlier springboard version people do use at theate request mapping instead of different type of htb method specific annotation let me show you let's say I want to use this request mapping and you need to define the URL users okay so that you need to Define as part of value then you need to Define here which kind of HTTP method is this if You observe there is something called request method method okay so you can Define method which type of method is this HTTP method dot which kind of action you want to perform let's say gate you can Define like this okay if you want to perform post you can Define like this rather than doing this or rather than specify it manually spring provided the updated annotations you can directly just Define the action as part of The annotation itself okay you don't need to explicitly Define the method type just play with the corresponding annotation okay so that is the reason this request mapping is the parent interface or parent annotation for all the individual HTTP method okay so let me remove this fine now let's move to the next one that is to check your Proficiency in HTTP methods and best practices for restful web services interviewer may present you couple of scenario based question for example let's consider a scenario can you design a restant point assume that you have a product database and your task is to create an API to filter a list of products by product type basically you need to write a endpoint where you can pass the product type as a input based on this product type it will filter from your DV the list of product object okay Bas based on the product type you need to filter list of product from the DV that is what the rest endpoint you need to design so let's do it from the scratch I'll go to the intellig idea let's go to the why there is error okay we'll just comment it out just go to the product controller so to make our job simple I have just Define a product service where I have just hardcoded couple of object name and this this is what the product type Electronics education baby and kids like that I have just specify the hardcoded list of product now here you need to write a endpoint from the scratch to filter list of product by product type so first step you just need to define the method public response entity then just Define something like gate products then just Define the input based on what you want to filter that is product type now you can simply call service dot get product by type and give the product type okay so the in this get product by type there is no complex logic this is very simple what I what I'm doing here just getting the list of product and just filter based on the product type and I'm just returning the list of object this is what the simple Java filter condition I have written here okay and I have hardcoded the list of product here it's just for the demo I just hardcoded the value just Define what it is returning list of product and now you need to Define return response entity the status code should be okay and just Define the result now the main key part what interviewer want to look from you is that how you are playing with The annotation now if You observe in this scenario we want to perform the retrive operation we want to give the input and we want to retrive the response from the server so I need to use the get mapping I can Define the URL here okay/ products let's say search something like that then what is the input you want to pass that is product type this this is the value based on that I want to filter now how you can pass this input you can pass as part of the request URL so there is annotation called path variable this will pass as part of the request URL just Define this and give the correct name what you want to pass as part of the input okay so this is what you need to write so here interviewer don't care about how you are writing the logic rather he will just look into the approach how you have defined The annotation and how you are giving the input to the endpoint that is what the key aspect what interviewer interviewer was looking from you okay now let's let's quickly test this particular endpoint let me start the server just go to the browser type local host or just Define 8181 okay then URL is products SL search now you need to pass the input right input is what the product type let's say I'm just defining electronis I'll just enter I'm getting only two object can you see here mobile phone and the keyboard with this price and this are the model based on the filter condition what we are giving we're able to get the result now let me try for different filter condition product service let's say education we are only getting single object because in our DV I mean the hardcoded list what we have defined we have only a single object of this particular product type okay this is how you can Define the filter condition using get HTTP method and this is what the piece of code you can just write now let's move to the next one let's discuss another scenario you need to design a endpoint in a way that it will take the input as a product type if the user provide this input the endpoint should filter product based on the specific condition if user is not giving that input then it should return all the object okay so for example in the previous scenario we have just given the product type and we got the result but what if user is not giving any input then in that case I want to return list of product available in my DV the whole list of product I want to display to the user if user is giving the input well and good I just need to filter based on that if not I can simply fetch all the record and can display to the user so this is what something you need to design okay so again this will prove about your HTTP methods how you understand it and how you'll play with the annotations and what are the best practices you are following okay so let's go to the ID we'll quickly design the same scenario so just Define a method public who return response entity I'll write a method find products then what input we need to pass we need to pass the product type as a input if user is giving this value then we'll filter otherwise we'll return all the product available in the DV so to pass that value first we understand okay we are going to perform the retrive operation so I'll just Define gate mapping I'll just Define filter slash I need to pass this input how I want to pass with different approach I don't want to use the path variable I want to use at theate request param okay and you need to tell to this request param what is the input this is my input so you want to make it mandatory or you want to make it optional so you can also do that just Define the value and you can Define is it really required I mean if you specify required equal to true then you must need to pass that value if you don't then required false because that is what our scenario right if user is not giving this then I want to return list of product object so that is the reason I will keep required as a false which will be act as a optional okay now we pass the value next I'll just do something I'll check if I mean I I can use the tary operator product type equal equal to null or if it is not null service dot get product by type using product type or else just return all the products fine what it will return list of product now finally I'll just break it down then I'll just return response entity do okay and give the result which is product list so this is very simple right we are just giving the input product type and we are just validating if the value not null filter it otherwise just return all the value from the DV now let's quickly test this out just restarted the server so it started just go to the browser I will copy this I'll paste it here filter then what is the input we need to pass if you are using the request parm make sure to just openend the question mark as part of the URL then Define what B you want to pass that is product type and which is nothing education okay just enter it we are getting one result here can you see here now if I'll give the electronics let me go to the service I will copy the EXA keyword now if I give electronis we getting two object okay but if I won't give anything if I will not provide any filter let's see whether we are getting the result or not we are getting all the available product can you see here this is what the use case we want to design fine so if you are giving the input it will filter if not you'll get all the result apart from that also if you want to keep some default value for your end point you can also do that let's say default value always I want to filter based on let's say Electronics you can also use the default value if you are not giving by default it will consider okay user want to filter based on the electron okay now let's move to the next question that is what is the difference between path variable and request parameter so if You observe in our code we have used both the approach so we have used the we are given the product type as part of the request URL using path variable we using the product type as part of the request URL using request param why there are two different approach why can't I achieve everything in a single approach or single annotation right so the key difference is that this path variable is mandatory if you won't give this input you will get 4 not4 server will unable to identify the URL or the resource okay if you won't give this particular path variable value but since we make this product type optional if you won't give there will be no issue so we'll just prove it right away just go to the browser this is the path variable approach right if I will not give what I'm getting here guys I'm getting 44 am I getting 44 here correct but if I am using the request param if I'm not giving it still I'm getting the result because I have just written the code like that and the main thing I have mentioned this as a required false which is nothing optional so whenever your input is mandatory you must need to pass that as part of the request URL go for path variable whenever your input or the request input is optional you are not forcing user to filter or you are not forcing user to pass that value then happily go for the request parameter so if You observe the simple use case if you open the flip card or menra homepage until unless you'll not filter the product type it will display different products available right all the products availability it will just display but once you do the filter it will display only the filtered result right so this is one of the best use case to identify the difference between path variable and request param fine now let's move to the next question that is why do you why did you use rest controller why not controller class that is why did you use r controller why not atate controller annotation so if you go to the code we have defined the controller class with annotation this one right with annotation rest controller so the question here why can't I use at theate controller annotation so basically interviewer wants to know the difference between atate rest controller and adate controller annotation okay so I have just created let me close this I have created something called controller demo and rest controller demo fine so let me Zoom this for you so Here If You observe in the controller demo class I have just defined the endpoint SLC controller / welcome and just returning the plain string similarly if you'll go to the rest controller here also I have defined rest controller / welcome as a endpoint and I'm just returning the plane string okay so let's try out this both end point then then we are able to find out the difference between these two annotation so let me copy this I'll just duplicate this and I'll just replace it here can you see here it's working right now let me try the other endpoint from the controller demo that is controller / welcome just go to the browser just replace it press enter we are getting 4 not4 okay okay so why there are difference whenever I'm just using at theate rest controller I can able to see the response but when I'm using the controller I'm not able to see the response rather I'm getting 44 so the reason is that if you are annotate at theate controller and if you have defined the end point then always spring framework looking for a HTML or view page for this particular string okay but if you have annoted at the r controller then it will return anything what you have just defined in case of controller it will always expect the return type as a model and view but here what we are returning we are returning the plain string so spring framework internally look for this hello a a page web page but if he is not found that is the reason we are getting 44 because there is no page with name hello right so to prove that what I'll do I'll just go to the pom.xml I have just added the time Leaf just uncomment it update the project and then I'll go to the resource template I have just defined the hello. HTML I'll just refactor it okay now if I run the code I'll get the response from this hello.html that is what I'm just trying to convince if you define at theate controller it will always expect as a return type model and view if you don't have that view present with this name it will simply cry with the 44 okay let's run it and we'll quickly verify it so started just go to the browser and I'll simply refresh this can you see here welcome message from the controller that is what we have just defined in our HTML page okay and the page name begin with hello so it was able to identify by The View resolver because the return return type if you have annoted at the controller it should be always expecting as a model and view okay but in case of raised controller the media type can be anything you can return any response okay now the question here can't I return any different data type from this controller class should I always return this HTML or some web page no you have option if you want you can return the string from this particular or any any data type from the controller annotation as well how we can do that so I'll just write something like message I'll just return something like welcome to Java Tey then I'll just Define atate G mapping slash message now here I need to specify what is your media type you want to return what you want to produce from this particular Endo I want to let's say I just want to return media type some plain text text plane value okay next you need to Define at the rate response body okay so in this approach you can tell to the spring hey even though I have just Define at theate controller annotation still I want to play with the different media type I have just specified what media type I want to play and please openend that as part of my response body rather than looking for model and view from this particular endpoint so let's restart to verify it it started just go to the browser just change it to message can you see here okay I have already defined the different endpoint with the message so I'll will just rename it to New or greting message fine just restart it just go to the browser and change the endpoint welcome to Java can you see the result here we got the response welcome to javat right so if You observe same thing if I want to achieve from the rest controller there is no changes required whatever the value I want to return I can able to return different media type but in case of controller I need another annotation called atate response body and if I have Define atate controller so that is the reason I am just defining the response body okay with the produces now if you want to technically try to find out the difference between this controller and rest controller annotation the first step controller always expect model and BV return but if you want to customize the media type you need to play with at theate response body now if you'll go and check in the rest controller annotation the rest controller annotation is the combination of at theate controller and at theate response body can you see here okay so this is pretty straightforward whatever you are achieving here using the controller and response body that is what the combination annotation build for rest controller can you see here controller and response body so whatever we are able to do using rest controller same thing you can do in atate controller by combining these two annotation okay so this is what the main difference between controller and rest controller fine let's move to the next one how can we deserialize a Json request payload into an object within a spring controller or within a springing rest Endo basically I just want to give the Json request and I want to convert it to the object for example I have this Json payload with me where it contains a object and having the field book ID title publication here and again this object contains list of authors okay so each author object again contains author ID name and worth year so there are there are two different object in the author's list so you need to design your dto class or request class with the same structure you need to create the object then you just need to Define these are the attributes then you need to Define list of author then you need to create author class with these are the attributes okay so I have created a d class let's go to that I have this book class with all the field what we observe in the Json book ID title publication year and list of authors if you'll go inside author we have author ID name and birth year okay can you see here all the field already defined in the request class book and author but you need to keep a note the way you have defined the key in this Json structure same key you need to Define as a variable name so if you go to the book I have defined book ID here and I have defined book ID here if I'll do the type mistake either in the Json request or in the D class let's say I have just defined book D capital and I am just giving here book with ID D small then I'll will get the bad request so make sure whatever the key you have defined in your Json same key should be your variable name okay now next this entire Json structure I want to map to this particular object that is what I just want to deserialize the Json string as a payload to this particular object how we can do that very simple let me write a method for you I'll write let's say public string okay now here what I need to take as a input nothing the book object I'll return something I mean I'm just getting the title and some string and then getting the publication here as a return return type so what annotation I need to Define here I have the input which is nothing the object so you must need to use at theate post mapping or put mapping okay and just Define the URL here let's say SL books now the main part what we need to do we need to map this Json object to this particular book object what I have defined in my end point so to do that there is a straightforward annotation given by Spring fairwork just Define define request body okay so by using this particular annotation we are instructing to the spring to deserialize the incoming Json request to this particular book object okay so that is the reason you need to give the correct key whatever we are defining the key and the variable name should be match okay now let's quickly verify this just go to the postman I'll just give the URL Books Okay the HDT method type should be post and I have this Json payload now let me send the request spring in action new book has been published on year 2022 fine this is what the return type we have now if you'll do any mistake either in the variable name and in the key you'll get the bad request fine let's move to the next one that is can we perform update operation in post HTTP method if yes then why do we need put mapping or put HTTP method the question is very simple okay so let's say I have one class that is yeah yeah demo controller so Here If You observe I have just defined post mapping and put mapping in post mapping I'm performing the save operation or I'm just inserting the new resource to the server in put mapping I'm just updating the existing server I mean existing resource in the server okay so post I have used post I have used here to insert the record Put I have used here to update the record now the question here can I execute this piece of logic inside the post mapping is this possible if yes then why there are two different HTTP annotation post mapping and put mapping yes technically you can perform update operation using the post HTTP method but doing so would violate the principle of rest pool design in rest each HTTP method has a specific purpose and semantic meaning the use of post is generally to create the resource however the use of put is just to update the resource and the main issue while mixing up these two operation is ID important nature of HTTP methods okay so let me tell you what is ID importancy principle in rest FL Services then we'll understand why it's not recommended to perform update operation in the post mapping or insert operation in the put mapping vice versa okay so let me demonstrate about the I impotency nature it's very simple statement on your subsequent action if the resource is getting modified then that is not considered as a I importent okay so here your post method is not idempotent but put method is I important in nature it means in the post request if I will subsequently access this particular endpoint it will always create a new resource okay it will always modify the server so that is the reason this comes under not importent or non importent but in put mapping on my subsequent request it will not do anything on my server it won't modify my resource on the server so that is the reason it comes under it poent so let's validate it quickly I'll go to the postman body I have something called name and age that's it okay so let me repeatedly hit this particular end point then we'll verify in the DV and we'll understand whether it is not ENT or ENT just send the request one time two time 3 4 5 five time I have triggered the same request which is nothing the post mapping and five time it create new resource on the server so if You observe here five times it created the new resource in my DV so that is the reason this comes under non itent but if I'll do the put operation let me copy this I'll give the id5 I'll change it to put Let It Be now if I'll subsequently call this particular endpoint there is no change on my server right if I'll verify in the DV there is no new resource added so that is the reason it called as a ID importent since we understand post is non important and now if you'll execute update logic in the post then always it will create a new resource Source rather than update the or modify the existing resource right so it's not recommended still you have option to handle post method to check if resource not exist then create otherwise just updated like that you can do the kind of filteration and perform different post and put operation but again it violate the rest principle so it's always recommended if you have insert operation blindly go with the post mapping if you have update operation happily go with the put mapping okay let's move to the next one that is can we pass request body in Gate HTP method let's verify it in the gate HTP method can we pass the request body let's say If You observe here this particular end point I have defined as a gate gate mapping SL user slame and I'm just giving the request body as part of this get HTTP method so is this possible to do yes technically it's possible to include a request body in a gate request but it's not recommended practice because you should always consider the alternative HTV method whenever you want to pass the request body like you can go for the post or put mapping rather than going with the gate mapping with the request body so gate mapping always recommended to pass the input as part of the request URL either encrypted or decrypted for that is up to your business but it's not recommended to use but if I run the code and if I trigger this particular endp there will be no error let me trigger this you're getting the result so again this is not the recommended way to use request body as part of this get HTP request but it will allow you to pass that's not recommended okay this is one of the interview question I mean they will ask you to confuse you because never you will do this kind of scenario no one will pass the request body as part of the gate so if you have not done it before if someone will ask you then the confusion comes to your mind right so this is the uh straightforward answer it's not recommended but it is not forcing us to stop the approach we can still do that but it's not recommended let's move to the next one that is how can we per from content negotiation in rest endpoint so I want to design my endpoint who can return both Json and XML or any different type of media type how we can design the content negotiation approach in our restful web services how I can design my endpoint who can return different media type that is what something called content negotiation okay so to do that let's go to the intellig idea I'll take any of the existing existing controller class let's go to the product controller okay so let's let's apply the content negotiation here so what all data types or media types you want to support for this particular endpoint I want it to return the Json I want it to return the XML as well okay let's do the change better let me copy this I'll simply return list of product I'll just remove the URL from here it can be accessed with the root URL / products I just remove this I'll just return it that's it so this particular rest end point I want to enable content negotiation for this so what all um media type I am supporting here let me just Define I just want to support produces application sljs also I want it to support application. XML so this particular endpoint will return the Json and XML based on the user input so to enable that go to the pom.xml you need to enable the XML data binding so go to the pom.xml and just you need to uncomment The annotation this one Jackson will do this kind of stuff for rest I mean for Json but for XML we need some dependency right so Jackson data format. XML fine just update it and go to the application properties file just enable this content negotiation specific key and value so if You observe I'm just defining spring MBC content negotiation favor path extension true parameter true and the the default content type is application / XML if you're not giving anything by default it will result you the XML okay that's it let me restart the application but before that we have done the changes on this controller right and we are giving the product type as a input okay let's restart it just go to this I mean this one right filter we have added product then question mark what format you want to you want as a response just define format equal to Json you'll get the result in Json if you want the result in XML you'll get the result in XML can you see here this is the XML hierarchy right the tree hierarchy but if I Define the format as a Json you'll get the result in Jon can you see here this is how you can enable the content negotiation for your rest Endo for different media type here we just play with the Json and XML but you can play with different HTTP media type okay let's move to the next one what all status code you have observed in your application okay this is most frequently Asked interview question can you please list down list of status code what you have observed in your project this is what something the interviewer can expect from you so to answer that there are different HTTP status who represent the state of response okay so I have list down couple of status code 4 which is nothing the bad request if you're giving if client expecting something and while accessing it you are giving something wrong input then you will get bad request which is nothing 4 then you'll get 44 when you'll get this 44 when the URL you are giving that resource is not available okay then you'll get the 44 then 41 is nothing about the authentication error if you are giving wrong credential to log to the system then you'll get 41 you'll get 43 if your authentication is successful you have given the correct credential but you are not authorized to access that particular feature then you'll get 43 authorization error okay next 45 so we will get this 45 as a method not allowed error so I have defined let's say I have defined a r end point with the post HB method and I'm trying to access it with the gate for example this is gate right I'll try to access using post I'm getting 45 method not allowed okay so if you'll mess with the HTTP method then definitely you'll get this error next 415 okay so 415 is nothing unsupported media type let's say client needs Json and you are sending XML or client needs some additional input and you are not giving that as part of your request then we'll get 415 unsupported media type for example let's go to any of the example so here I'm just giving the Json because that is how I have designed my endpoint we'll take the input as a Json and we'll convert it to the object but let's say I'm changing the media type here I'm sending the XML okay whenever my server expecting to be Json okay now let me send the request I'm getting 415 unsupported media type so next one is 50 internal server error this is a generic error message indicating that the server encountered an unexpected condition that prevented it from fulfilling the request and this is the common error message usually we observe in our project next one is 502 bad gateway let's say you have one application which is acting as a upstream and it is unhealthy for some technical fault and you are trying to access it then you'll get 5 not2 bad gateway okay next 20 which indicates okay the request was successful the payload in the response will contain the request data okay and next is the 2011 means created the request has been fulfilled and a new resource has been created that is what the status code indicate about the resource creation so there are many more HTTP status code but these are the status code which we frequently observe in our project fine let's move to the next one how can you customize the status code for your endpoint so just to you understand right 20 means status code okay two1 means status code created for example if You observe go to the demo controller we have just defined a post mapping to create a new resource and what is the response we are getting let's verify it let's say this one right body this one send the request we are getting the status code 20 however when we trigger this request it create a resource on my server so I should return 2 not1 instead of 20 right I need to return 2011 how can I customize that very simple just go to the end point and just Define response status what is the status code you are expecting you can Define created okay I'm expecting this method to be return return hdtb status created if there is any error you can set HTTP status to the internal server error which is 5 okay let's run it and we'll verify it's up now just go to the postman and let's send the request can You observe what is the status code we are getting 21 created you can also see the popup right the request has been fulfilled and resulted in a new resource being created so this is what you need to follow the best coding practice to just implement the corresponding status code for operation for example put in put also we are just returning 20 right so here in the put you can return return 2 not4 which is nothing no content so for example go to the P you can Define response status no content okay you can Define like this so these are the some basic standard to Define your HTTP status fine let's move to the next next one how can you enable cross origin in your project so first of all cross origin resource sharing is a security feature implemented by web browser to control web page request made from one domain to another so using this cross origin you can restrict what all host or domain can access your application for example let's say I have just defined this Endo okay now I want to access this particular Endo from my angular application so I need to allow my spring boot or I need to tell to my spring board hey if from the angular you are getting the request with this particular server with this particular protocol then only allow them to access this particular end point that is what you can enable using the cross origin so the approach is pretty straightforward just annoted here at the rate cross cross origin and then Define from which origin you will allow all the request let's say my angular is running on Port HTTP and it's running on host Local Host and port number is let's say 40 tri0 okay so here I'm telling by this particular annotation I'm allowing that particular whole post and port and this particular protocol to allow my backend application all the endpoint whatever Define in this demo controller can only be accessible from this particular integration I mean from this particular end point okay this is what you can enable the cross origin so this is not the only way to enable the cross origin you can also play with Java base configuration if you'll go here go to the config cross config I have just defined to save our time I mean whatever we have just defined I'm just adding here in a Java Bas config okay so here if You observe Ive just defined add cross mapping I have just overr this method and I have enabled the mapping for/ API SL star any request coming to this particular pattern must be enable for accessing and allowed origin I have defined if the request is coming from this particular host and Port I will allow if you want to allow for all just Define the star and what all method they can allow get post put and delete if I have any other method which is which I have not listed here will not be accessible okay then allowed credential true I'm just telling that okay the client application can apply the credential as part of the header or as part of the request okay that's fine next the key attribute is Max age okay so by defining this dot max age 36 I am specifying the maximum age in a second of the pre-flight request when I say pre-flight request it's just an initial request send by the browser to check whether the actual request is Sav to send or not okay and I have just specify the time to validate this pre-flight request okay so you can play with Java Bas configuration or you can simp simply just Define the cross origin annotation allow for specific um port and host or specific protocol or just allow for all okay that's how you can Define the cross origin let me comment this for now and also I will just comment this for now now let's move to the next question that is how can you upload a file in spring or spring boot so it's very simple file is not a raw object it's not a employee or customer like Json or raw object rather it is a binary content so to play with the binary content spring or spring board provided a data type called multiart file data type or you can use The annotation called at theate multiart file to play with the file upload kind of scenario for example let me show you one demo I have created One controller class file upload Okay so If You observe here I'm taking the file as a input and the type is multiart file okay and then I'm just setting the destination path where I want to store the file which I'm getting from the request file. get original file name and I'm just returning some messages after the file has been stored to this particular destination path do just ignore about this piece of code Our intention to understand how the file upload will work in the spring so if you are using this multiart file then that binary file will be converted to this particular object and then from this file object you can get the original file name and its bytes and everything you can write wherever you want okay so let me run this I will enable the time Leaf because I have used the controller annotation and I have just created some HTML page to just Define the result what the pom.xml just enable it then simply start the server but before that let me verify the end point G mapping slash so let let me Define something like home okay because there could be a default URL with Slash as we have defined so many controller that's fine let's restart it for now go to the browser just change it to home can you see here file upload example choose the file let me choose something like Java PNG open now I need to upload it file has been uploaded successfully if I go to the desktop and if I validate can you see here the file is just uploaded this is what we just uploaded right you can choose some different file let's say I want to upload uh this one and upload it now if You observe the file is uploaded okay so this is how you can work with file upload in Spring framework just by defining the multiart file okay that is what the key what interviewer can expect from you how you can upload a file what data type you can use so you can simply reply that you can go with the multiart file data type fine let's move to the next question how do you maintain personing for for your ra API so basically maintaining versioning for your ra API is crucial to ensure backward compatibility while introducing new features or changes so there are several approaches to versioning the r API and in spring board there are couple of strategies by using you can maintain the versioning for your R API to support the backward compatibility okay for example let's say I have designed an application called flight booking kind of scenario there I have defined one method and in future I want to change the implementation on that method which is already in life then rather than do the code change on the existing method I can create a new version of it okay since it is on live I don't want any trouble or I don't want any bug in the production rather I want to create a new version of that same feature so that is what something called versioning and how we can can maintain that let me walk you through one example and there are strategies let me walk you through all the strategies just go to the ID so I have created okay let me close everything Ive created a controller class called flight booking controller okay so it's very simple guys okay so I have written this particular endpoint I have just written this particular endpoint booking ticket it will take the input and it will process the actual logic then finally it will book your flight ticket now assume in future I want to enhance my booking ticket feature so what you can do you can write the code here business logic in the same existing code and you can redeploy it but is there any guarantee even though you have done full testing is there any guarantee that you might have have missed a small Corner case which can break your booking ticket uh particular end point in production right so we cannot guarantee about that there could be n number of corner case which might be missed at the time of development even the tester was not able to identify that so rather than taking the risk always it's safe to maintain the versioning so just Define okay this is what the B1 endp and I will Define another end Point called B2 and here you just write the so in this case this will be as it is live people can still able to access with the/ B1 SL book now and this is something the new version of your booking ticket implementation right so I'm just giving the example of this but there are different way you can Define the versioning the first way I have just shown you by just defining the path URI okay next approach you can Define the versioning as part of the request parameter itself for example book ticket versioning with request parameter you are giving some request object and in request parameter I'm just giving the version if it is a version one then execute this if it is a version two then execute this in future if you come come up with the multiple version then just do the conditional statement to execute that particular logic okay this is what called header versioning sorry this is what called request parameter versioning now the next approach is versioning maintain the versioning based on the header parameter so in the header you can specify the API version and again it is kind of the conditional execution if version equal to one then do this if it is two do this I mean this is for old this is for new if there are any further new announcement comes either you can do the the do this version condition or just go with the else approach okay so there are three different approach you can maintain the versioning based on the path URI based on the request parameter okay this one and based on the header parameter so let's verify this okay so I will change it to the new flight booking success okay new flight booking approach or something like that okay now let me restart the server we'll verify all the possible way to maintain the versioning so let's trigger this particular Endo I'll go to the postman since this is the post request so the first end point B1 I mean which I handle on the E itself V1 flight not here right after flight send the request flight booking successfully completed this is the let's assume this is the B1 version one of my flight booking feature now I have enhanced the two vers two and I'm sending the request new flight booking approach successfully completed now I need to pass in the request parameter I mean I can play with that request parameter what is the value version then version one if I will give it will execute this is version one of the resource if I give version two it will execute this is version two of the resource now next I can pass as part of the header so we'll try that out maintain the version using header parameter booking now two this is what the end point I don't need any request parameter just remove it what I need I just need to maintain this us using the header what is the header key API version just copy this paste it here as part of the header this is version one of the resource if I'll change it to the two this is version two of the resource this is how you can Implement multiple strategy to ensure your backward compatibility using this versioning okay cool let's move to the next one that is how will you document your rest API so we we are done with the development I mean we are done with some critical Business Development we have defined couple of rest and point I need to document them somewhere right so that I can give it to my client or whoever want to consume my application or who whoever want to test my application I can share those document to them so how can I maintain proper documentation of it how can I define all the HTTP method what is the input it needs what is the HTTP method it is being used and what is the status code what is the Response Code how I can document everything okay so to make our job simple spring boot provided Swagger implementation earlier it was Swagger now it is called open API 3 so using this open API 3 implementation we can document our rest endpoint or we can document our all the end points with zero code okay let me show you how we can do that just go to ID go to the project go to the pom.xml first you need to add this dependency this one org do spring do open API starter web MBC UI you need to add this dependency then everything will be done behind the scene by Spring boot itself so what I do since we started the server let's go to the browser and I'll just trigger Swagger I can see the Sager documentation here now whatever the controller we have defined it contains information about all the controller it documented correctly demo controller flight booking controller we have just defined this end point right B2 ra controller Mo product controller demo controller demo user client controller and these are the schema I mean these are the input we are giving to our endpoint user details it contains this input author book I mean everything is properly documented so let's try some endpoint okay let me try this users this is the post request right I can try it out from here so where is the option can I edit here no click on try it out and give the value ID is optional I'll give the value Bon age I'll give something like 35 now execute this request we're getting the response 2011 which is the status code and this are the response body we found and if you verify in the DV there the record has been inserted right so using this open AI it's not only you can document the endpoint you can also do the quick testing of your endpoint okay this open API is the specification of uh open API 3 recent version and if you want to generate the API description you can simply use let's say I'll just copy this let's say I don't want to go through the documentation rather I want to share my API description to the user I mean whoever want to consume it or some other team so I'll simply do like this um B3 SL API hyphen it should be B3 can you see here this is what my API description from this description you can read I mean what is the name of the controller what is the HTTP method what is the request body it is accepting user details I mean what is the status code have been configured everything whatever you are getting in this user interface same thing will be converted to this Json key value but if you want to download it just define y ml it will download the document for you API hypen dos 1. yl okay like this you can document your all the rest endpoint using the Swagger open API now let's move to the next one that is how can you hide certain rest end points to prevent them from being exposed externally so if You observe in the SAG documentation all the rest end point being exposed to this particular interface Now demo controller flight booking controller everything is just published here for example I don't want some of my end point to be exposed using this documentation how I can prevent that so if you'll go here let's say flight booking controller I don't want this B1 and B2 to display in the OR to expose as a documentation so how I can do that for example let's take this one so there is a straightforward annotation you can use hidden okay so if you'll use this particular annotation that particular end point will not be exposed as a documentation so for example let's check some different endpoint go to the product controller I also don't want to expose this go to something else file upload okay these are the three different Endo I don't want to be exposed as part of my documentation so how I can do that we have just use at theate Hidden and also if you want to specify what this particular endpoint is doing if you want to add some short notes then you can do that for example let's say this post SL users right it is there in demo controller so here I will just use let me Zoom this for you I'll just use at theate operation and I can Define what operation it is doing insert okay you need to Define okay this is what the description that's it okay now if we will start the server we have defined this file upload as a hidden this particular endpoint flight booking this B2 which is the new feature I have just disable it and anything else okay the product controller fine well it started just go to the browser and I'll just refresh it now can you see slb2 in the flight booking controller because we have just disabled that right in the flight booking controller this B2 we have just hide it or we are just forcing it to not expose okay so using this atate hidden then in product controller the content negotiation example what we have done let's verify that that is not there here right only two rest Endo is visible then file upload okay that since it contains only one Endo so we mark it as a hidden so that section is itself is not visible okay all good right this is how we can hide your endpoint to be exposed publicly let's move to the next one that is how will you consume restful API so in Spring framework there are multiple approach you can consume the restful web services you can use the rest template you can use the Fain client you can use the web client and in the recent spring boot 3.2 version there is a option called advanc ra client using all the four you can consume any restful web services okay so let me walk you through example of each and every approach to consume the web services so let's assume I have an endpoint which is kind of mock response which will give me the mock response let's say this is what the Jon placeholder type code com/ users this will give me the list of mock user okay it will give me the 10 user details with field user name email address everything okay so I have created corresponding dto in my project so we are going to use this particular endpoint to play with rest template web client and Fain client okay so how you can how I can consume this particular endpoint rest endpoint using race template it's very simple first let me go to the class where I have defined all the appro first you need to define a be of this race template okay in your config class just create a bean of race template then just simply Auto add it and you just need to consume that particular endpoint by giving the URL and by telling that what it will return list of class of type user response and what it contains ID name username email address phone everything what you can see in the response I mean I only want to capture name username email I don't want address this nested object I don't want so whatever the field I need I have just capture as part of the response can you see here and to consume using the race template you need to focus on three things what type of method HTTP method you are consuming that you need to keep in your mind so Here If You observe this is the gate right we are just retriving the response so I have just defined get for object if it is something post then you can go with the post for object okay and what is the URL and what is the return type so let me access this particular endpoint who will consume this particular endpoint and will give me the return response okay so what is that F mock user one let's go to the Postman new HTTP this is what the end point right I mean this is what my Endo from where I'm consuming the rest mock user using the rest template I'm getting the response okay so here we got the mock users from the endpoint fine now let's move to the next approach that is Fain client so this basically Fain client is a declarative web service client developed by Netflix and integrated into the spring Cloud ecosystem it simplify the process of making HTTP request to a remote service by just creating a proxy for that particular service as a interface okay when I say proxy as a interface it's very simple so if You observe I just we just we are just consuming this particular endpoint right this is the gate endpoint so I will just Define a client endpoint by specifying this is what the URL and I just need to enable the Fain client then everything will be done behind the scene I just need to call the method rather than again in Booking the way we are doing in the race template okay you don't need to explicitly specify the URL you just Define your own proxy who will create the code at wrong time and consume the event for you I mean consume the API for you for example you can see here this is very straightforward approach now I just converted that particular provider URL as a rest end point I mean I'm just playing with annotation nothing else I've just Define user client and I just want to use the Fain client to consume this particular Endo okay I mean this is the parent endpoint and I have just given the name of of it and/ users okay so nothing guys there is nothing to confuse let me copy this for you this is the URL we are going to consume right this is the parent URL what I have specified and this is the URL to access that particular endpoint I have just represent that rest API as a method as a rest endpoint in my client code can you see here by using this pain Cent there are so many advantages you can play The annotation and by default it enable the load balancing this Fain C okay and at the wrong time this Fain generate code at run time based on the annotated interface based on this interface Fain client generate the code at run time this can lead more efficient and type sa code isn't it and the testing is so dead easy because whatever the client you have defined just call that it's it's dead easy right I I'll show you that and service Discovery Fain integrate with the service Discovery mechanism such as urea allowing your application to dynamically disc discover and communicate with other services in the ecosystem by just this name okay there are so many advantages of using this Fain client now wherever you want to use this user client to access this particular endpoint will connect to this server and get the details just inject that okay so we have just injected here user Cent and simply call the method see here it's I mean it's single line of code okay let me call this particular endpoint user two send the request we got the response let me trigger it again we got the response right now the next one is web client so basically this web client is used for reactive programming to achieve asynchronous and non-blocking consumer implementation you can go for the web client so it's not recommended to use the web client for traditional implementation code I mean whenever you are not using reactive programming you cannot I mean you can use web client but it's not recommended because anyway for the traditional if you're using web client you must need to block that request right which who does not make any sense to use web client but if you have something the Rea programming it's good to go for web client let me show you the example of web client nothing just create the B of web Cent I'll will show you in the configuration simple just create the bean of web client by defining the base URL then just simply use web Cent the H method type is get and this is what the URL to communicate retrive the result body to flux user response. class okay just convert it to the flux and response collect list. block so why I have blocked it because this what I have written is the synchronous call if I will use the reactive programming web flux I know need to block this okay so here I'm just blocking and getting the result since this is the synchronous call for demonstration purpose but this is not the recommended way to use the web client so now if I'll test this I'll get the result we got the result right let me hit again so we got the result so there are many way based on your use case you need to choose what exact template or what exact implementation I need to use to consume the web services and the fourth one is Advanced rest client so you can check out my latest video I will share the link in video description so that you can play with this Advanced rest client since I am using the different dependency version I need to rever it back and I need to add that piece of code so that is the reason I have not added it but I've created a separate video for using this Advan R client okay so that's it for today's video I will come up with more questions on upcoming season until then enjoy the course and feel free to leave a comment if you have any doubts that's all about this particular video guys thanks for watching this video meet you soon with A New Concept