Write Your First Microservice Using Java and Spring Boot | Java microservices | REST API

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
foreign [Music] so right now what we should be doing today is we will be start writing some small small Services okay so we will not go into spring Cloud pretty much today but let's say we are starting spring cloud from tomorrow when I say we are starting spring Cloud what I said about about spring Cloud I told you that spring Cloud will give you some features to develop microservices and it will help you to fix the challenges that we usually have in microservices and when we develop microservices so to fix those challenges to understand those challenges that we usually face when we create microservices first of all we need to develop some micro Services right so in today's lesson what we will do will set our stage we'll create couple of microservices we'll make sure to communicate those micro services with each other and then we'll try and understand the problem that we usually have when we deal with microservices and we start bringing in Spring cloud and one one projects from Spring cloud and we'll see like you know how the spring Cloud basically help us to fix and design um you know different things and well we'll go into that okay so as I said today's goal will be pretty simple it is to you know create couple of microservices so this is the first micro services that I want to create we'll create a employee service and we'll create an endpoint right over here called slash employee slash ID so basically this particular endpoint will be a get call and this is going to help you to get an employee based on an ID so you'll provide an employer ID this is going to give you an employee back where the ID will be matching so this particular service that we will create we will deploy that in a tomcat server and of course we will be creating this application using spring boots that we don't have to deploy it manually on the server because Tomcat is inbuilt so the moment you'll be starting the employee service by default it will be deployed on a tomcache server and then only it will expose this endpoint that you will be creating okay and uh obviously you know we'll have a database which is going to be our employee database and this will be connected to our server this is the first thing that we'll be doing and also we will be creating another microservice and this is going to be our address microservice where we'll be creating a endpoints slash address slash employee ID so whenever you're gonna hit this endpoint and provide an employee ID this is gonna face in the address for a specific employee where the ID matches and basically gets an address based on the employee this is something this endpoint is going to do and also we'll make sure to you know have it deployed on a tomcat server and also will make sure to connect it with a database which is going to be address database as I said its microservices will have their specific you know databases your employee will have employee database address will have address database and then this is something that we'll be trying to do today we'll try to develop these endpoints last employee slash one where one is the ID if you see closely this is the endpoint that we'll be developing and we are hitting right now and employees slash one is gonna get you the employee detail right here and it is going to tell you the employee ID name blood group email and also if you're gonna see it closely there will be an address attached with this employee like for this employee avillas this is his address like Lenny something Lynn to is something state is something Jeep is something right so if you're gonna see it closely here this is our employee database this is our address database whenever we are hitting this endpoint employees last ID we're gonna get this detail ID name group blood group email these four things will be getting in this database but right now we're going to make a call employee slash one so this micro Services right now internally call and another micro Services which is going to be your address microservice and gonna fetch the address detail from this database okay and that's why whenever we're hitting employees last one it is not only giving you the employee details and also getting you the address detail and this address we are we are basically internally making a call whenever we're gonna hit this end point it's going to be internally making a call to this end point and gonna fetch in the address data attach it with the employee response and gonna get you back so this is the response I'm expecting whenever you're gonna be hitting this endpoint so this is something that will be try and build right now and to make it pretty simple first of all what I want to do let's use only one database for all my microservices okay but we'll be having couple of micro services but the data for now let's store it in one database and this is an anti-pattern okay because usually every micro services will have this own database but right now let's say we have one database in both of the micro services are connected with this but later we're going to be changing it okay so let's start creating our microservice I think the requirement is clear so let's start creating our employee micro Service First [Music] thank you so I will be going to my springboard application and as I said this the employee microservice will be a spring boot application so let me start creating a new spring startup project I will call the uh project is employee service okay and com dot selenium Express dot employee app is going to be my group ID 30 fact is employee service looks good description I don't need any description let's say employee service is going to be my description and this is going to be my package as well okay and it's going to be a maven project Java version let's change it to 15 uh we don't have any 15 here this final uh like you know it will be any half pointing to 17 or 15 we'll be changing it later then packaging it will be a jar packaging and you know language will be Java yeah good so next so we will be going for spring wave so maybe later in the course we'll start using reactive wave and web flux but for it now we will go with spring wave as we are comfortable building our restful web services using the normal spring web MVC right the next thing that we'll be doing is going to be our dape tools let's add our depth tools as well and also we will be connecting to our mySQL database so we will have a MySQL driver and also I want the database connection the session Factory everything will be created by my spring itself and for springboard we have a beautiful starter called Spring data jpa which is going to help me giving some out of the function out of the box functionality to have some crowd operation uh so it will help me whenever I'll be facing my employee data or whenever I'll be saving my employee data I don't have to create methods and it's going to get me some out of the box functionality so right now my employee service is created and by the time it's getting created uh first of all let's try to do something let's go to our database and we will be using the selenium Express new database so this is going to be our schema so let me make it my default schema and right now I'll open this schema and obviously I'm using my MySQL workbench only right here and yeah so the first of all let's have a employee table so I'm gonna say it employee and we're gonna have few columns like ID of the employee and then the name of the employee email of the employee and the blood group right blood group okay so ID will be um you know integer type and I will set the auto incrementation as true name email blood group all will be let's say a string type I think this is what we are getting in our response can I go to the requirement and let's see the requirement in a little more detail and uh ID name blood group and email this is good and address one we will consider it later first of all let's say this is our employee table and let me start creating one right now by hitting the apply button right so I got my employee table created here and initially let's have a data one name is a village email is uh let's say can I drag this in a little bit and let me say a village at the rate gmail.com and the blood group let's say O positive okay let's say I have something like this to and apply apply close uh there we go so we are good so we have one employee in our database and let's try to create a endpoint right now in our springboot application to face in this data into our springboot application so first of all this application that we have created whenever we'll be starting this we need to make sure that we are getting connected with our database so first of all I will try to open this properties file okay so next thing that I will be doing here inside this properties file I'll be setting my data source information so data source dot spring dot data source command show me the properties yeah there we go URL and also I'll be setting up the data source dot username and next one spring dot data source dot password these three things let me set and the driver name driver class if you want you can set it by using spring dot data source.driver class name but I think we have MySQL in our class path it will automatically figure out my driver and put the MySQL series or jdbc driver on the driver like test the driver it will be using internally I believe so right now um you know the data source URL let me just point to my data source that I have created it will be this schema that will be pointing to where my employee table will be there so first of all let me copy the connection so copy jdbc connection to the clipboard and I will go and paste it and the schema that we are using right now is this inside this we have this schema called selenium Express new right so I will copy this schema name control say it paste it over here that this is going to be my URL and the username is I think root and the password is Avila so I think I'm set with my connection information so whenever I'll be starting with this application whenever I'll be start this application a connection pool and a connection will be created out of this information and that will be done automatically by the spring data JPS startup package so you know it all happens automatically through the order configuration like the connection Factory session Factory Connection object a data source object all will be created out of this information I don't give a damn like you know how it is creating so I'll do I'll just to a control s right now I think we are all set the next thing that we'll be doing once we are set with this particular thing let's do one more thing Let Me Go To Source main Java right now and let me start creating my endpoints so let me create a class and this is going to be my controller class where I will be creating my endpoint uh this endpoint slash employee slash ID this endpoint I'll be creating right now so for that I'll be needing a controller and this is going to be my employee controller right simple let me just create a employee controller and this is going to be my rest controller so let me have the rest controller right here and the first thing here that I'll be doing that I will be basically let me just zoom this in a little bit I will be basically creating a method public uh let's say I'll just give the method name get employee details uh get employee details okay so let's say somebody will get me a employee ID like this it will be a get call so it will be a get mapping and right now somebody will give me this last employee's so as I said the URL should be always plural so make sure it you should not call it employee whether you create an endpoint by giving a name as employees make the you know the main component name is plural to follow the best practices now employees and then someone will give me the ID which will be dynamic that's why I'm wrapping this off with a color press the next thing that I'll be doing I'll be capturing this ID with a path variable so somebody whenever they will be giving me the ID over here with this URL I'll be capturing this ID by using this path variable and the name of the path variable is ID whatever is written here paste it over here then you store this value inside a integer int ID so we'll be capturing this idea right now with the help of this ID right now will be looking for the employee that we have inside the database let's say somebody will get us the end point uh like you know somebody will hit this endpoint rather commenting it out over here I will open up my Postman as well and in my Postman let me open this up and in this app what we should be doing let me open this up and let me just put it some here here okay and right now this should be the URL should be localhost 8080 where my application will be running and let's say this will be employees it should be employees slash one and now we should be get back our employee one data that we have in our database right here employee one data is a village we will try to face in that data and show it over here in form of Json so this is what we are planning to do so this one will come over here we are capturing over here and now let's make a DV call right here and fetch the data of employee one okay so now this is very uh easy to do first thing that we'll be needing to make a database call first of all we need to create our entities so let's create a class and this is going to be stay inside a entity package and this class is going to be my employee entity and right over here in in this entity class first of all I'll make it an entity okay and I'll make sure that this is where we are pointing to our table which is called employee so my table name is going to be if I'll go to my database my table name is going to be employee control C this you can see we have a table here called employee let's go back to our STS and just let's just keep the name as employee here so right now we are creating a entity where we will be mapping our database column name that we have over here to our Java Class Property so I'll copy all the field name I'll go over here and I'll place it here and now I'll start creating the you know properties for all these things so I'm just pasting it over here just to make sure that you know I am reading it well and I'm I'm not doing any mistake so private int ID is going to be my primary key private string name is going to be my name and private string email is going to be my email and private string uh blood group uh blood group is going to be my blood group first of all this ID will be a primary key so I'll make it a ID just to make sure that this is a primary key the next thing it will be the generated value I will be setting it as identity when the ID will be generated by my database I want it to be mapped with this guy one second let me just stress out a little bit and let me use the strategy as identity so that the ID will be mapped to this guy right the next thing that I need over here I'll be using my column name and my column name is going to be uh like you know name is equal to this guy ID so I'm making sure this property is going to be a mapped to this column in the database now this property will be mapped to this column in the database I think we all are good with this right this is like you know some basic Java uh you know hibernate stuff or JPS top that you are doing over here and the next thing that we'll be doing over here is that we'll map all the other columns that we have will map the name Will map the email will map the blood group let me just copy the name paste it over here copy the email paste it over here just to make sure there is no typo I'm doing this and copy and pasting the thing if you uh like you know trust your spelling don't do this okay you just directly type the name or something so the next thing that I I will be doing over here I'll click on source and I'll generate the getter and setup for everything I'll click on select all do generate right now my employee entity is ready and right now I should be able to make some database call to fetch an entity or face an employee entity from our database and whenever I'll be facing a new record the employee data will be mapped to this values whenever we'll be facing it from the database so all our database values that we have right now right here all will be mapped to this particular variables that we have over here in our Java class so that we can use it in our application now let me close this and let me save this thing obviously now right now we will be making a DB call so for now let's go ahead and create a you know Dao right here and in Spring boot we don't have to create a DA or right right away I will you know create a repository and I'll say it as employee repository okay the employee repository will extends to jpa repository let me expand this and this will be exchanged to jpa repository jpa repository do I have it oh employee report is going to be a interface right so it is going to be interface because the implementation will be created uh through the proxy in the runtime whenever it will extend to jpa repository and right over here you will be specifying the T the T as your entity name and right now we have created an entity called employee I'll map it over here and inside this employee entity the ID is basically a integer type so let's go over here and set this ID as integer but the generate cannot be a primitive so I will write integer right over here okay so my uh you know implementation for this is going to be generated right away in the runtime and I'll be able to call the jpa repository related method like save update page find and everything in the runtime so I'll do Ctrl s right now so right over here I'll close this class I'll close this class as well and right here in my employee controller right now we can straight away make some dial call by writing private employee repo and employ a report here all right and I can make it auto where okay and I can start calling the employee repo related method like employee default dot find by ID and you just pass in this ID that I have over here and this is gonna get you if I if I'll do get it's gonna get me the employee object back that I will be returning assign it to the local variable and there we go I got my employee object back now I can return it over here and I'll get back my thing this employee object I can return it over here and there we go I am ready when I will be getting my employee related information whenever I'm making this endpoint call right here on my Postman it is going to get me the employee data by making a database query but ton 24th line it was a type of oh okay return employee okay anyhow I'll not be returning this employee object right over here uh why is that because this employee is basically an entity right and we will not be directly returning the entity uh because this is going to be a thread to our application if you're going to be directly exposing your entity objects to the outer world right now if I'll be returning like you know like you know employee over here this is all good I can just maybe I can give a space over here and I can write employee and right now there we go we are done and our application can compile here and we'll be getting our employee data back whenever we'll be making this endpoint call right over here but this is not going to be a good pattern so what I'll be doing here I will I I will be making sure we will not be returning a employee entity directly rather will be returning a model object We Will We Will paste the data from the employee object and we will be setting it to a model which will be our response on subject which will be similar as our entity and then we will copy the data from employee object paste it to our model object then we send it back to the outer World whenever somebody is making a call like this so those things I'll be actually doing in my service layer and you know that in your controller you should not make any database call okay so we will be moving this thing anyhow to the service layer so first of all let me create a service package and I'll write service package over here and I'll make it as employee service and inside this employee service right now I will be moving uh the database calls to here so here I'll be actually making a database call so I'll say public and right now this is gonna get me a employee object back whenever I will make a call let's say get employee by ID okay we'll have a method like this I will basically uh you know maximize it and I will return my employee object here whenever I'll make it become so those couple of lines that we have written here on our controller let's move it to our service layer right here and also if I'll go to my controller this Auto bearing thing I'll move it to my service layer right here okay so now I'll also make sure I'm using autoware here so my service I'll annotate it as ad service so it will be a stereotype annotation and right now employee service will be eligible for component scanning and whenever the beam power this will be created the injection will happen automatically for employee repo and the spring will do it by default and now here only we are making a DB call so what what I will do here right now this get employee by ID will basically accept a parameter and this parameter we will be sending it to this and this is gonna get me a employee object pack now I can return this employee object right shot okay so there we go and now this is looking good so now this service object we can basically Auto add in our controller so Employee Service employee service we can create over here Employee Service everyone please be on mute make it E small and please be on mute everyone autovad and there we go and this employee service using this employee service right now we can make a call like get employee by ID and whatever the ID we are capturing from the URL we will put it over here we'll make a call this is going to get me a employee object back and I will say it employee and I will be returning this back let me first of all see this application is working right now or not as right now we are just directly returning the employee which is an entity but later we will change it to a model object right now first of all let's test this out so employee controller do a control shift or to fix all the Imports there are some problem here some here because I don't know because I haven't saved it right now things are good let me just start this particular service and let's see if it is working uh right now or not okay so later we will start fixing by by doing by following some best practices here now it is started on port number 8080 so now let's just go ahead and let's just hit our endpoints employee slash one and we are going to get a uh get a problem here called method not allowed because this is a post method and this employee slash one that we have defined over here is a get method so if you're gonna go to your controller this is a get endpoint so let's go ahead to our Postman and hit this endpoint one more time and this time we'll change it to get before we hitting this endpoint and now we're going to be getting back our employee object here [Music] foreign [Music] here that we are basically returning our employee object directly here right which is an entity so I don't want to return an employee object rather I will be creating a employee model so I will make sure to create a new class and I'll put it inside a package called response this is going to be my response subject and I will make it as employee response right and inside my employee response right now I will not do any specific thing first of all let me just stop my console app otherwise it will start reloading every time so right now I will make sure one thing um this is my employee uh entity what I'll do I'll copy all the variables from here Ctrl C8 paste it right here okay one second I'll copy out all the variables from my entity to my response okay and I will make sure my entity variable names are same as my response variable name this is not an entity class so I'll be removing all the database mappings that I have for my columns and there we go so now I have ID name email and blood group and I have ID name email and blood group right over here okay so now we got the employee response here we have all the variables right over here as well and I'll create the getter and setup for everything I'll select all I'll do generate and there you go we should be good with everything all the variables let me cut it and move it to the top and now we got our employee response right here cool so we are good right now do a control shift or to remove any unnecessary Imports and now we should be good now what I want inside my employee controller instead of returning a employee object directly I'll be returning a employee response because I do not want to expose my entity object to the outer Vault so I'll do employee response and I'll make sure this employee object over here instead of returning the employee I'll be turning the employee response over here so this conversion I will be doing it inside my service layer so I'll go inside my service layer and right over here whenever I'm making a database call here I am getting a employee object right so what I want to do in service only I want to convert this employee that I am getting to a employee response right employee response and to do that first of all I'll change my return type to employee response and now I will make a conversion here first of all let me make it simple let me just create employee response employee response here and is equal to new employer response and then let me just manually set everything employee response dot set ID and ID I'll be getting it from this employee because this is where I made a database call and I'm getting my data over here from my database so I'll be featuring like get ID over here and similarly I'll set the other thing I'll say employer response dot set name and I'll do employee dot get name and I will do the same thing for the other couple of variables that I have employee response Ctrl C paste it over here Ctrl V and I will also set the blood group and I'll get the blood group from the database here only I'm at the database call I'll say employee dot get blood group and right now I'll return the employee response instead of employee which is my employee object right now I'm returning my employee response which is the response object which we will be actually exposing to the outer world all right I think this is making sense right now okay now yeah no it is a good practice to create a mapper here only right now before introducing mapper I'm just using it in this way I will change it in two minutes I'll use model mapper here okay I mean like I I know you taught bottle mapper but I've seen like a simple mapper also they are creating like a simple class Network yes exactly 47 exactly exactly you can create your own mapper here as well okay if you don't want if you don't trust like Maps truck or model mapper which I popular mapper here which is going to help you to let's say I have thousands of properties over here if you don't want to map it like this uh from one object to another object by doing some manual coding like this obviously you can create your own mapper if you don't trust the third party vendor but right now I just did map everything by myself and I'm returning my employer response and I'll go back to my controller and here this is right now no longer will get you a employee employee entity directly whether it will get you if I will do control 1 assign statement to a new local variable it is going to get you a employee response pack just directly return this employee response right over here and one more thing Let me just remove this thing Ctrl s and I think we are good with our code Ctrl shift or to remove any unnecessary Imports and we should be fine with our API development the next thing that I want to do obviously this thing will work good only one thing I want to do right now obviously it will be a good practice to use a response entity a response entity here instead of sending our object directly I want to set some status code manually so that like you know this is my body this is my payload that that I am returning whenever somebody is making a rest call but along with this rest call if I want to control the status and the various different properties then the response entity will be a good thing as I have discussed in the rest sessions so here what I will do instead of returning the employee response directly that we are doing as that we are getting from a service call I will use my response entity here so response entity and I will just use dot status and you can use for a get request I can give a status okay and then the body or the payload is going to be this response that we are basically getting whenever we are making a service call and this service call here is making a DB call right here finding the employee and then we are converting this employee to a employee response subject and giving it back to our uh in a controller and our controller is sending this particular response to the outer Vault by using this endpoint and exposing the employee response data along with the status code okay okay okay means if you're gonna hover on this it is going to be 200 so do a control s relaunch your application and let's see that if the things are working good right now we have started our application to ascend and we are getting our things right now the status is 200 okay and the people who are new to response entity don't worry uh like you know if you might be thinking okay what's new over here you are still getting 200 okay because I have set the properties right over here you just change it to something else like uh you know create it let's say if it is a post request and you are saying created then obviously this will be right now changed to created over here 201 created so that's why I used a response entity so that along with my buddy along with my payload that you are seeing over here I can also control the status code and that's why I have used the response entity where I am setting my status code but this is a get mapping and for the get mapping I will I will basically give okay as the status code fine the next thing that I'll be doing one bad thing that I am doing right over here let me stop the application inside my service layer I will be I I'm basically converting my entity object to my response object manually okay so let's try to let's say if there are thousands of properties it will be very impossible to do all these things by manually doing all these things so we need to create a mapper so as akhil said we can create a mapper by ourselves but here I'll be using one of the popular mapper that we have in Java called Model mapper you can also use map struct if you are planning for that but here I will go to map and repository and I will just get the model mapper installed with my application so I'll just look for model mapper and I will copy the properties of model mapper here and I will go back to my springboot application go back to the perm.xml and right here I'll be pasting it Ctrl B Ctrl a control shift F Ctrl s is ready so far let me have an update just to make sure that we get our dependency in our class path and the next thing that I'll be doing here I will be closing my form.xml I will start creating a model mapper so I'll just have a model mapper instance here in my service class I'll say model mapper model mapper model mapper and I'll get it auto where obviously this object will not be automatically created by boot because there are no Auto configuration happening for this the model mapper being you got to create it by yourself but we'll do it in a little bit of time but first of all right now this model mapper instance let's use it over here and we can basically right now uh you know remove all this thing we don't need to do all these things manually so here we are getting our entity back whenever we are making a DB call we are making a repository call so here we can use model mapper so model mapper dot map and you can see there are methods with text the source and destination the source is going to be this is the source this is where we have our data filled in whenever we are making a database call and this data we want to map it to a class called employee response right so I'll say employeresponse dot class right now what will happen so all the variables that we have over here ID name email blood group all these properties right now will be mapped to this class properties right and this will be happen all automatically right now if you're gonna see this property names are matching ID is matching with this name is matching with this email is matching with this right now we are getting this object back whenever we are making a database call and now all this data that we have inside this variables will be mapped automatically to this we don't need to use a Setter just the way we have done previously so in our service right now this mapper will map the map the data that we have over here into this class object and it's going to return me a class object of if we're going to assign statement to a new local variable of the employee response so all this sort of thing we don't have to do right now and we can simply delete it okay and now this there you go we got the employee response back and now if you're gonna start our application let's see if our mapper is working fine okay we have a problem because right now some autoware is failing if you're gonna look to the console closely it is saying the employee service is having a required bin called model mapper and model mapper is not found because the model mapper that we have Auto add over here and trying to use the functionality of the model mapper of the map method this is not we are not able to call this because the model mapper is null right now and we don't have a bean present so make sure to create a configuration file right here and I'll create a new class and this is going to be my employee app config so let's say employee app config and this one is going to be my what we call that config package right and right here inside my config package I will have a configuration annotation so that it will be picked up my picked up by my dispatcher servlet and the dispatcher sublet will help uh creating the beans that I'll be defining inside my configuration class and basically I will have my model mapper being defined here model mapper model mapper and there we go and I will make it a bean and here I will return a model mapper object okay a return a new model mapper object new model mapper to a control shift or have the Imports and there you go I think this time we are good let me relaunch the application and this time our applications start off fine okay and there we go we have the port number 8080 context path is right now not set and I think things are good and now let me go to my restful um application Postman do I enter and I'm getting my employee data back right so all the good practice are right now we right now we have followed and we are done with creating the first part of the things that we are planning to create over here this data right now we have now we need to get the address response data by creating another address application or address service application but first of all tell me if we are good so far whatever we have done pretty much boring thing to do because something that we are already familiar with but tell me right now if everyone over here is clear off clear with this thing like Whoever has not attended my all the sessions of spring Boot and all the other stuff you guys are also good with this or not because this is some very basic thing can I just get some answer from you guys yes no so far we are good yes yes yeah yes from yes yes yes why model my first bin has not been created before the configuration yeah because like model mapper is a third party API Supriya and we do not have any auto configuration class written for model mapper so the model mapper beam has not been created by springboot and as you can see the pom.xml we have added the dependency by ourselves it's a third party API so we should take the responsibility of creating the model mapper beam it will not be created for you if you're gonna directly use autoware it will be fail injecting the beam because there will not be any beam found for that okay cool uh got three things on the chat hello sir gm good morning Felix it must be uh a create B in one second I'm getting some calls here okay uh it must uh is it must to create a bean for model mapper es Felix is much if you're not it will not be creating it will be a bean creation like you know it will be a the auto wear will be failed because the beam for model mapper will not be automatically created so we are done creating our employee application now before we do something else first of all let me stop the application and now let's try to create another service right now and let's start building this application right here address application how much time we have um 819 okay in 10 minutes can I do it let's see that so let me start creating another microservice let me create a import uh sorry spring startup project as I said the address microservice is right now is going to be a another micro service that we should have and this is another application that we should we should deploy it inside another server and this is something that we'll be doing right now so first of all let's create a address service address service and this is going to be the application name and let's say it is going to be address app is going to be the group ID and the artifact will be address service and description is address service and the package name will be address app okay there we go and I think all the things is looking good right now we can go for spring wave my install driver Dev tools data jpa all the same things do a finish and let it create the address application and uh by the by the time is create the address application also will be using model mapper here so let me go to the previous application form.xml file go to the employee service and here we have the pump.xml and we have copy pasted our model mapper let me copy this guy as well and let me call this go to the address service and paste the model mapper here as well so our dependency has been set for my address Service as well and I will make sure I will just collapse the employee service I'll go to the address service I will do a right click Maven update project just to make sure we are updating the project and we should be good with this okay so there we go so address service right now we are done with this with the basic setup and the dependency and the next thing that I'll be doing here I'll be going to uh my table and I'll create my address table here so I'll create a new table first of all and this is going to be my address table and we will have a address ID or let me just say ID here this is going to be my primary key okay something on the chart uh can we remove the dependency version from the model mapper uh yeah spring boot will handle it I I will be coming back to this uh particular thing Santos but for now we are having the version number of the model mapper because as it is a third party API that's why we are maintaining the versions inside our pump.xml for model mapper spring Will Spring boot will be automatically doing it yeah that thing I'll be showing you on the parent palm and also if it is not there in the parent pump what is the good practice of you know maintaining the person number also I'll be letting you guys know whenever I'll be adding the other dependency okay for now I'm closing it and going back to my address table okay so right now I'll create the address column right over here I'll also make sure uh this is going to be my primary key of my address table called address ID the next thing that will have is let's say Lane one there are some column like lane two there are some column like uh the state there are some column like um the Jeep right and there you go do and apply apply close and there you go right we have our address table okay one more thing uh we think we need to do here uh how come somebody will know let's say there is some address called one lane is a sample sample Lane one uh Len 2 is let's say a sample Lane 2 state is let's say GPS let's say seven six five six seven seven okay so now if I do apply how come somebody will know that this particular address is basically mapped to which employee so for that we need to create a one-to-one relationship by specifying a employee ID right over here as a foreign key so for for now I will just try to alter my table so can I do a alter table and in my address I will have one more column here called employee ID so that you know you can track this address belongs to which employee and this is going to be a foreign key so Mac it an integer go to the foreign key section and try creating a foreign key for address and this is going to be FK underscore address uh underscore sorry let's say FK underscore employee underscore ID this is my foreign key name and now we are inside the address table so this foreign key is going to be referring to the employee table that we have and in our address table we got this column called employee ID in the address table we have Lane 1 Lane 2 State Jeep and also the employee ID this is going to be referring to the ID column of the employee table now if I'm going to do apply this is going to help me to create a foreign key constraint do an apply close and right now we should have a one more column right there called employee ID and right now I can specify the employee ID right now here as one and also I'll put a sample Jeep here just to make sure that we are specifying a chip do I apply apply and close and now I can see this address is is belongs to the employee ID 1 which is right now of the Lash because the employee id1 is basically pointing to a village so now let's just try to make sure one thing let's go to the employee application that we have created earlier let's go to the resources section and from here from the resources let me copy all the DB properties that we have set copy this close that uh collapse it now go to the address service and go to the resources section go to the application.properties file and right here paste your properties right so that whenever you will be starting your application you will be connected to the selenium Express new repository uh that's good so next thing that we'll be doing from here will be start creating our you know address service right here so here we'll be creating a new controller and we will be just doing it as like you know we'll put inside some controllers package and it will be like you know address controller okay and inside this address controller right now we will make it a controller let me just expand this this will be a rest controller and the next thing that I'll be doing is that I'll be creating a public it will give me a address response just the way we have created the previous one and right now this will be get address by employee ID so somebody will give you an ID of an employee that you will be getting through the parameter here as a path variable and this will be uh like a employee ID that somebody will be giving let's say employee ID and you will be capturing this int ID and now this is going to be my get mapping okay and now the mapping will be slash address slash and some Dynamic value and this is going to be the dynamic value which we will get some when somebody will hit the URL okay so the next thing over here is going to be you know what we will be returning a address response right now so let's create a address response here so let me just create a new class hover on this click on new class and right here I will say controllers and I will put it inside a model and this is going to be my response let me just say response and now this is going to be address response and now this is going to be my variables let's say private int ID this is going to be my address ID anyhow private string Lane 1 and private string Lane 2 in private uh what what else we have private string Jeep or private long Jeep let's say GP is going to be an integer and also we have state private string State this is going to be the response that we will be basically giving it back when somebody Max a API call that is this one okay so there we go so it's good so maybe right now I can do one thing I can just save this particular address response here and I'll create the gator and Setter for the same I'll click on select all do generate in my getter M setup for the address response is created okay so the next thing in the controller I'll just create a address response object let's say address response address response uh and I will initialize this to null and now I will return this address response okay obviously now we will fill in the data of address response right now by making a database call this is good so right now the next thing that we need to do here this address response will not be directly returning it it should be a response entity as as I said before so response entity and I will map it to this guy okay and now here I will just say response entity dot status and status is going to be HTTP status dot OK and now the body is going to be this like this object that I have created right here and obviously this is does not having any payload right now but we'll be facing it right now by making a database call fine so now before we make a database call let me just create a address entity as well so let me just go ahead and create a class and this is going to be my entity class okay and the name of the class is going to be address right here okay and first of all I will expand this and I'll make it an entity so I'll do at entity and the add table and I will point it to the address table so the name of the table is going to be address so I will go to my database and this is going to be the table right address so let me copy the table name go back to my application paste it over here so the next thing is that I will just have columns for everything so I'll just do right click copy Fields name go over there and paste it and now let me just create all this particular variable for the same I'll go to my address response right now copy all this maybe I'll copy everything that I have here Ctrl C go to my entity class and paste it and now I will start giving it ID here for my identity key generated value will be I didn't generated value will be a strategy will be identity because I am using MySQL here and then I'll have my ADD column for this at column and my column is going to be this guy ID oh sorry yeah ID so now I will I'll paste it for a few more times okay Lane 2 chip State okay and I think we are good now I will just replace my variable name here Lane 1 Ctrl C paste it as line one and then lane two okay there are spaces in between why I kept spaces in between we should not keep spaces anyhow in the database column name Ctrl C this is going to be the state and Jeep Ctrl C and this is going to be the Jeep for employee ID we don't want to create a one-to-one relationship right now whether we'll make a DB we'll just I'll just tell you like you know how we will find the employee ID for now you just ignore this employee ID column that we have inside our database right so now this is the address entity and these are the value that I am expecting the ideal and one lane to Jeep State whenever I am looking for a particular address inside the table right now let's just create a repository so now first of all what I need right now in my repository I'll go to my address application I'll create a new package and this is going to be my repository package and inside my repo package I'll be creating a new repository interface so this is going to be address repository address repo okay and the address report okay this address report is basically an interface so I'll change this class to an interface and this is going to exchange to jpa Repository okay and now what I want jpa repository now the type of this is going to be addressed this is the entity that we have just created address entity and the ID is over there a integer okay there we go now what I want I want to find a address based on a ID so let's just write a query right here to create to find a address based on a ID based on a employee ID so how can we write a query if we need to if I'll tell you to write this requirement or to write to write this particular write code for this particular requirement I will be finding it anyone any idea over here just help me to create this find by then let's say find by unless a find at let's say I will create something find address by employee ID I want to write a manual query so how I'll be writing it yes employee ID is a parameter so somebody will give me the employee ID so I'll write it param param and the param name is let's say employee ID okay and this is like you know I'll I'll store it like in a string employee ID okay now what I will do I'll write a query here manually okay and right now let's write the query first of all everyone please be on mute I'm just go ahead and try writing a query here so let me just go over here now I will just try to join the address table that we have here that we have the address and the employee table that we have the employee I will join them based on the ID and to to write a join query first of all let's say I'll go to my address table okay now I got all this data so this is the table I want to join okay this table name I'll just make it as employee address EA I will join that with uh employee table so I'll copy this and paste it Dot employee if I'm going to join this on employee ID let's say on EA dot EA dot ID the employer sorry uh this is employee table right this is less than e where e dot ID is equal to EA dot inside this we have employee ID copy field name paste it over here let me just run this let's see that what data we are getting so we so right now I just performed a join so that you know where EA I like you know employee ID column and employee address table employee ID column is matching I am finding all those data so I'm finding the data for a name email blood group these three columns is coming from my employee table and these uh four columns that we have or five columns that we have we are getting it from the address table and one more thing that I will do here I'll have a where clause and I'll say where EA employee ID is equal to let's say we will provide the ID here as one so if I'll do run this the query is running fine I do not need all the data here I only need EA dot ID EA means address table EA dot ID I need this ID and then EA Dot Lane 1 let's say Lane space one I think we have to give spaces for that okay I'll do one thing guys in the address table okay I have done a blunder I will just remove this space that I have between the lane 1 and Lane 2. okay and I'll do apply apply and close I'll go to the address table right now yeah the column name has been modified and now I'll start giving EA dot Lane 1 comma EA dot Lane 2 uh comma EA dot State comma EA dot chip right now let's run this okay now we are getting the address where the employee ID is one right so this is going to be my query that I'll be writing okay so I will just move it to one line this is my native SQL query where there we go and I think we have written our query do a control C go to your application your spring application and right now inside this query give a value for this query and paste this one okay and now this employee ID is equal to 1 this employee ID from where we'll be getting will be getting it from here so this is going to be my parameter value right I'll do Ctrl C this and this ID I will just dynamically Get It Whatever somebody will provide so if you're gonna provide here one like this is going to be a integer value if you want to provide here one this one is going to be replaced right here okay and I think things are good right now and this is going to get me a address object back okay there we go so I think we are ready and this is going to be a native SQL query so I will also make sure I'll say native native query and I'll set true okay and I think we'll be right now able to get whenever will provide a employee ID this is gonna get me this all these columns from my address table Yeah ideally in one lane to State and Chip right and also inside the address column I'll remove the spaces because I did remove it from my database as well okay and I think we are good right now I will good with the repo so I'll close this right now I can start creating a service class so I'll do right click new and I'll create a class and this is going to be in my service class okay and right now in my service class I will be giving it as address service okay and now I'll do a finish this is my address service and at the rate service and right now I'll create a basically a method public let's say it will give me address response address response address response okay and this particular method will be building now we need the address response this will give back the address response right now and this particular service method I want to call it from my controller right here so this address response I can fill in and I can send it back as a response right so to do that I'll go to my service class right now and let me design this one right so address service and right now this is going to be a obviously this particular thing will have a employee ID the employee ID will be capturing from the URL from the controller and will send it to the service class whenever the controller will call my service method and the next thing that will be doing here is that we'll make a repo call so we'll do address report address report and we will make it auto word okay and we'll call this right now address report Dot and what is that find employee by employee ID and pass in this ID this is gonna get you back at address entity okay and I'll say it address and now you want to convert it back to address response because this method returns address response and for that we'll be needing a model mapper so private model mapper model mapper and make it auto wear make sure to create this Bean as well or to where and there we go and make this particular variable as private as well okay now we are good now let's use the model mapper to convert your entity object to the response object map and right now the source and destination this one is going to be a DV call it's going to get you all the address stuff and now I want to map it to a address response address response dot class okay there we go do a control 1 assign statement to a new local variable and there you go address response now just send this particular guy back control B and return this okay there we go so now so right now we have our address response ready so let's just have a call of this particular service class method in my controller so let's go back to my controller and in my controller right now get this guy Auto where so just say private address service okay make it auto bad okay there we go and import the address service right here by hitting Ctrl shift o now just make a service call you can just do let me just trace this out a little bit and right here let's do a service call address service dot um what is that address response what is this ad oh this should not be address response let's say find employee by employee ID this is going to be my method name all right let's call this guy from my controller and pass in the ID and this ID will be capturing right here from the URL okay and there you go I think we are done and now this address response you assign this guy with this that should be fine at least okay find it find address by employee ID correct find address by employee ID there you go Ctrl C Ctrl s and go over there paste it here as well and paste it here Ctrl s okay all good so let's test our application okay the model map will be in we need to create right so we will go to our app and we will create a config class and this is the last thing that we will be doing right now so this is going to be our config this is going to be our address config and do a finish and right now uh this is going to be a configuration class okay and now I will just create a model mapper being public model mapper because the model mapper being we did not create and we're using model mapper so you have seen that it will give us a problem it will not create the bin so let's create the Bean over here and let's make sure we are returning a new model mapper object there we go Ctrl shift o to fix it and we are all set let me close everything let me click on this and do right click run as springboot app and let's test our microservice application right here is started and now if I'll go back to my post main right now it is also running in 8080 port number right and I'll say address slash m I'll give the employee ID and the employee ID is one let's do Ascend and it is finding me the employee ID one address over here now what is my goal and what we will be doing tomorrow is what I need right now whenever will be will be calling the employee slash one it will not only get me okay eight zero eight zero okay my employee application is not running right now whenever let's say let me stop this application and right now whenever I'll be calling my employee service right now run is spring boot app okay now my employee service will be called I'm starting my employee service let's say whenever I'll be calling this endpoint employees right do Ascent with this response it is also going to get me the address response by making a internal call to the API whenever we'll be calling this endpoint right now in my employee service whenever we'll be hitting this endpoint right now in my controller internally this endpoint should make a another call to my address microservice this is you can see where we have this class inside the employee service so internally will make a call to the address service and right now inside the address Service uh it should hit my address controller this endpoint and gonna get me the address response back and we will set that response along with our employee response and and that employee response will contain the address as well and this is something that we'll be doing in the next class so in the next class we will actually get started with the rest call microservices communication and also we will involve spring cloud from Tomorrow onwards and I will tell you some uh you know uh you know the rest client that will be using which is going to be uh something what the rest client will not be rest template will be using a fan client and fan client is something that spring cloud is giving us to make rest calls and tomorrow we're going to be having rest calls and we're going to be communicating application to application between this couple of application that we have created um you know we'll be having rest calls and will be you know calling from here to here and we'll be using a fan client to make rest calls which is which is basically provided by Spring Cloud so I'm wrapping it up right now obviously it's more than one hour but yeah I got to develop this couple of services so that we can actually get started with our microservice right now okay so far you guys are good like nothing complex here ever like you guys understand everything right can can I get some confirmation please yes yes yes everything is good no like guys I know this is boring I know I'm sure it is boring if you have already understood boot rest and everything this is something you know should be very simple right so I think everyone who is watching this recording right now should be comfortable with this I did not make it complicated it was too boring for me to code even but yeah from Tomorrow onwards this is going to be interesting because as we will be start adding spring cloud and microservices over obviously right now okay one good thing is that right now you have created couple of microservices one is your address service one is your employee service right so two applications you have created and whenever you are starting this this is running on its own Tomcat server whenever you are starting this it is running its own embedded Tomcat server so congrat congratulations on creating on creating two micro services but yeah the micro service to microservice communication part will be taking care of that tomorrow all right okay previously used for the communication exactly previously we have used rest template for communication template is an outdated one exactly HTTP client we have we have web client remind me to remind me tomorrow uh Sydney was this point I will tell you why a rest template is bad why spring is thinking too in a duplicate rest template because rest template is a blocking call and whenever you are dealing with microservices you know you cannot basically block the entire application because the rest template will block the entire thread and your web server will not have thousands of threads uh to deal with request remind me to you know tell you this particular point tomorrow anyhow tomorrow what we will do we will first use rest template to make the call I will tell you why rest template is bad and then I will introduce fan client okay and the web client I will tell you on our web flux class whenever we'll talk about webflogs and reactive programming I will tell you why web clocks and web web client is coming why it is recommended to use spring wave flux over spring wave MBC I'm not telling you that don't use spring web NBC spring webmbc is still pretty much you know in the market you you will be using it but yeah reactive calls and the web client um web client we should use in microservices pretty much because we will get the data asynchronously we'll we'll talk about this tomorrow will not complicate right now it is good to have multiple microservices sharing One DB this is a anti-patron Manish uh this should not be recommended I will be telling about like you know you can have two microservices sharing One DB I have seen many instances here I am doing it to make it simpler for the people who are getting started but yeah later what we'll be doing we have MySQL and I think I also have postgres we'll be using this couple of database and will be start creating uh data like you know you know we'll be starting using this couple of database for our different microservices so yeah creating DB One DB for a couple of micro Services is not recommended but yeah it is anti pattern possible also been used by the microservices but yeah you know if you follow the good practices not recommended okay so yeah any other questions from today's lesson address Repository right it is used to change the name of the people uh address Repository at the rate of param yeah go to the address repository at the rate of param is yeah it's basically says the name you can you can write anything over here and make sure that anything should be going here okay so that whatever data you pass here wherever you are calling this let's say you are calling it from the service class whatever data you are passing it will be mapped to this and then data will be mapped to this variable and this variable will go here chaitanya yeah any other questions um also microservices communication is HTTP calls as per my understanding yes absolutely does my yes yes money is absolutely microservices architecture is completely HTTP calls and is rest calls and that is one of the biggest advantage of using microservices IF you compare to other architecture like SOA where we where so architecture right now people may be confused I'll be telling you guys about solo architecture as well let's start doing some coding and let's go to the architecture stuff later but so if you if somebody knows over people who have around five six of years of experience or 10 years of experience you should be well acquainted with SOA in SOA also we have ESB um you know we used to have mule USB Oracle USB and there we used to have calls using soap and that's why that was the demerit of that particular architecture as soap calls are heavy weighted but for microservices we are using rest calls and these are simple HTTP calls sir I remember doing a project with model mapper on Spring boot without manually creating the bean uh are there two types because it worked fine I don't understand Felix can you please pick like you know what you want to ask uh I remembered Philips can you repeat your question please I do not understand mic is not oh okay I remember doing a project with model mapper on Spring boot without manually creating the bean are there two types because it worked fine I don't understand anyone understood Felix questions [Music] oh is it so oh okay okay is it so Felix I do not remember like you know you tried it or I did try it in your batch maybe the 2020 or 2021 bus I don't remember Felix um yeah maybe just show me any instances if I have done it but I think model mapper will not be Auto configured it you should be doing it by yourself because it's the third party API all right any other questions guys from your end all good okay no questions any questions from your end chandrakant aparna Anusha yeah [Music] no okay [Music] foreign [Music]
Info
Channel: Selenium Express
Views: 41,894
Rating: undefined out of 5
Keywords: Java microservices, microservices, spring boot, Spring Rest, REST API, webservices, spring microservices project, spring boot rest api tutorial, rest api spring boot, microservices vs api, restful, springframework, spring cloud, java microservices, spring boot microservices, spring microservices, selenium express, microservices design pattern, microservices architecture, microservices architecture spring boot, microservices using spring boot and spring cloud, Selenium Express
Id: 9XI8lknaJzM
Channel Id: undefined
Length: 79min 48sec (4788 seconds)
Published: Sat Oct 15 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.