Placeholder Image

Subtitles section Play video

  • Hi.

  • Welcome to Amigos Code.

  • My name is Nelson.

  • And in this crash course, I'm gonna teach you everything you need to know about Spring Spring.

  • But it's an amazing framework for building Java applications very fast.

  • Recently, Netflix just announced that they will be switching their entire back end to spring book.

  • And this is because bring boo is so solid that there's no need for you to reinvent the wheel so they provide things such as connected to two databases jetty, servers, metrics, configuration and privileges Review.

  • Having to configure the low level code to get up and running, you can simply use spring book and get off the ground very quick.

  • So first I'm gonna show you exactly how to do Sprinkle the prentiss injection and how to implement to an interface where we will have any memory database.

  • And then I'm gonna show you exactly how to connect to a real database running on Docker.

  • So went out further ado.

  • Let's get started.

  • All right, amigos.

  • Welcome to amigos code in this video on a teacher Exactly how to use spring put 2.0, Spring bow is by far one of the best frameworks for building applications in the Java world.

  • So you gonna build an enterprise application?

  • You can use spring book or if you want to prototype on idea and see whether on and, you know, it works that you can use spring but because it makes it very easy toe build applications and basically springboard provides you a platform for building back and applications and on Lee that you can also build Web applications as well.

  • So in this video, let's go ahead and build this awesome A p I where any clients can submit a request.

  • So these requests could be a get request post the lead and put.

  • And then we're gonna take that request and within the AP, I layer or control layer.

  • So this is where we received the requests.

  • We're gonna send that to the service layer, and basically, the service layer is responsible to handle all the business logic that you may have within your application.

  • After you perform some business logic, then you might decide to use a database to perform some various current operations and basically so this layer right here so this layer is responsible to connect tow any database off your choice So the coating about spring book on Depends injection is that you can start with an implementation.

  • And then if you want to switch to a different database, you simply have to change one line of code, and I'm gonna show you exactly how to do that with the pendency injection.

  • So after you submit the actual request from any clients of the clients could be an IOS app, a react, or even an 100 up.

  • The request will go through all these layers and then go back with a response to the client.

  • So the response could actually be, for example, a Jason payload or an image or a status code off 205 100 so on and so forth.

  • So if you know, land the best framework for building job applications, stick to this video because there is a lot to cover.

  • And I'm pretty sure that after this video you will be very comfortable building back and applications with Sprinkles.

  • So let's go ahead and get started.

  • So the very first thing that I want you to do is to navigate to this website started spring.

  • Though I owe and basically this website, you can see that it's its title.

  • Spring initialize er it basically allows you to Butch trap your application by picking dependencies and configuring the actual project.

  • So go ahead and generate a project with maven.

  • We can see that it could be great.

  • It'll and then you have the option to code with Java chortling all groovy.

  • So I'm gonna sit with Java and then for the actual version, make sure that it is above two point.

  • Oh, so I've got a video on 1.5, and basically I just wanna make sure that you guys have the latest version so that you can use it within your latest projects.

  • So then go ahead and click on this link right here, So switch to the full version basically right here You can configure your project matter data, so go ahead and change these according to your company or project name.

  • But for this tutorial, I'm gonna leave everything as is apart from the Java version.

  • So I'm gonna pick driver 11 because I do have installed.

  • But if you don't have to have 11 and go ahead and pick Java eight because it will also work, then if it's called down you can see that right here.

  • We have a bunch off the penance ease.

  • And this is why springboard is pretty much one of the best job of frameworks for building applications.

  • So they give you a lot of flexibility with these appendices, which simply means that you can install one of these appendices and start coding right away instead of you having to configuring and, you know, setting up things.

  • So, basically, you just have to install these dependencies and then start implementing whatever you want, so you can see that they have a bunch of sections right here.

  • So web.

  • So service Web applications with spring envision Tomcat, Jersey Web sockets, template engines, security.

  • So they make it very easy for you to configure security within your applications.

  • So you know, it saves you the hassle of having to implement security that right ways.

  • So basically, they're doing the difficult job, which is implementing the security and and then giving you this nice package that you can just plug and play.

  • And then you have SQL.

  • So my sequel, a studio, BBC, post grass, fly away for database migrations, mongo.

  • And then you can scroll down.

  • You could see that they have a lot of great things.

  • So for this video right here, let's go ahead and focus on picking this dependency right here.

  • So Web server, web applications, wheat springing greasy on Tomcat so much that and then generate a project.

  • So right here you see the off court, this demo zip, which I'm gonna open in my desktop.

  • There we go.

  • So now I'm going to quick on this zip folder.

  • And if I the late the actual zip, you can see that now I have this folder right here.

  • So for this video, I'm gonna be easy intelligent, which is by far the best i d.

  • And if you want to learn about intelligent, go ahead and check my video on YouTube, which I show you how to download, install and configure intelligent it properly.

  • And if you are using net beans or eclipse, this will also work.

  • So insider Intel A J.

  • So I actually recommended to download and install intelligent if you don't have it to follow along.

  • And I'm pretty sure that you're gonna learn great things with intelligent throughout this video.

  • So what I'm gonna do now is pray much click on, open and then navigate.

  • Would that stop and then open up this damn a folder and then open up the actual palm thought XML So I'm gonna open that up and then open ice project, so I'm gonna allow and just keep a second maven A's resolve indeed.

  • Appendices for this project.

  • There we go.

  • So that's nice and done.

  • So let's go ahead and explore this project.

  • So open up demo and inside them or you can see they have couple of folders.

  • But the ones that we really care about are the palm dot xml So if I open it up and see that this is what you saw when configuring the project with spring initialize er so you saw the group I D artifact, um, name and then version.

  • So this is the version right here.

  • And then this is job 11.

  • Basically, the actual dependency that we picked was this one right here.

  • So springboard start a web, and by default springboards ships with spring ball started test for writing unit on integration tests.

  • So go ahead and close this because we don't need it and open up the source folder so open up, source and then Maine, and you can see that you have a job, and then inside you should have a, um, class called demo application.

  • And inside of this glass, you have this public static void main method.

  • And this is how you pretty much just run your spring application.

  • And if I play so make sure you play this, Just give me a second.

  • You should see that I have spring up and running and you can see that it say that tomcat started on poor 80 80.

  • So right here, we could change the sport.

  • But this is a D for port for Tomcat.

  • So I'm gonna stop this, and you can see that everything is up and running.

  • Fine.

  • Come on, stop this and then collapse this And then inside of resources, you have static templates and application that properties.

  • So static is where all the web resources live for Web applications, and then you have templates.

  • And this is where you have all the templates for your wear application.

  • So usually you would use something like most ash or E j s.

  • And then you have this application that properties.

  • So this is where all the properties for your application do live.

  • And the coating about properties is that you can define properties for different environments, so you could have a demo environment, a test environment and a production environment with different values for the actual properties.

  • So it's pretty cool.

  • So I'm gonna counsel out of that.

  • And now that you know the actual structure and make sure that you can start the application, you've successfully managed to start a Tomcat server with spring book.

  • So now let's go ahead and implement our A p I with all of those resources controllers Service's and finding a database as well.

  • So if I go back to this diagram, so remember that I said we had the AP layer controller or control that layer and then we have a service later for business logic and then database for data access.

  • So let's go ahead and start things right here.

  • So we're gonna start right here, and in fact, let me put a dot right here so you know what we're doing.

  • So if I remove that, basically, let's put just a dot so we know where we are, um, with things.

  • So right here and then if I collapsed That So basically, we're going to implement this section first and then move our way up.

  • So basically, we're going to implement one step at the time and then see everything working, fine, and then implement some other functionality.

  • So let's go ahead and go back to intelligently.

  • And the first thing that I want you to do is to create a MME a package.

  • So let's go and create package and right here simply call it a P I.

  • So this is where the FBI will live.

  • Go ahead and create a second package and simply say model.

  • Go ahead and create 1/3 package.

  • Call it service.

  • Go ahead and create 1/5 package, actually, fourth package and call it so we need the actual d A.

  • L.

  • And I think this is it.

  • So we have a B i D e o model and service.

  • And basically these packages are representing this diagram.

  • So if I go back and perhaps you haven't seen me, like right clicking so new and then package.

  • So basically, I tend to use a lot of keeper shortcuts and this is how I am productive with intelligent.

  • So if I press control and then enter.

  • You can see the keyboard shortcut down below, and basically, then that gives me the ability to create a new package.

  • So the same if I'm, for example, create a variable.

  • So if I say far and then a low equals to two, um and then if I press command and then why you can see the keyboard down below?

  • Right, So I've just deleted that line.

  • So also, let me go ahead and say, OK, so now what we need to do is actually the fine our model.

  • So what is the actual model for this application?

  • So if I go back to this diagram so it's going and define a model, someone copy this stereotype and you can grab this diagram, you can find a link in the description below.

  • We can download and see exactly you know, everything that we're doing here.

  • So basically the actual model soaps.

  • So the model for this, um, tutorial will be a person, and basically a person will have an i D.

  • So when I d and let's keep things simple and half like, let's say fool.

  • Oh, actually name just like that.

  • So obviously you could have more properties, but I want to keep it simple.

  • So you see exactly what's happening.

  • And so things you know become really obvious to you.

  • So we have this model right here called person.

  • Let's go ahead and define this in actual quote.

  • So go back to intelligence and inside off the model package.

  • Go ahead and create in your class and simply call this person, right?

  • So the kind it's a class and then press enter.

  • So now simply go ahead and say private final, and then the i d will be off, type you it and simply call the I d.

  • Go ahead into Pacific private final and then string.

  • So this is the actual name, and then I'm gonna add these two instructors.

  • You can see I'm using keyboard shortcuts again, and then the getters.

  • So just like that, and there we go.

  • So now we have a model that we can work with.

  • So if I go back, so now we have to define the actual database section right here, right?

  • So let's define the actual interface that will allow us to have any implementation for our database.

  • So inside of the d A.

  • Oh, go ahead and create a class and then simply say person and then d A.

  • O So instead of a class changed this to an interface, and this is the actual interface where we're going to define the operations allowed, or the actual contract for anyone that wishes to implement this interface and the quilting about it is that we can then use the penance injection to switch between implementations with just one line of code.

  • So let's go ahead and define our very first method.

  • So for our database, we want toe insert a person into the actual database, so we will simply return an integer.

  • And basically we're gonna mark a database using like, a list.

  • And basically we will return zero or one, depending whether the actual data is persisted to the actual database.

  • So let's simply go ahead and say, insert and then person, and this will take you it, which is the actual I d.

  • And then a person just like that, and then end up with semi column.

  • Now let's also go ahead and create a default, um, method and basically this will be the same thing.

  • Actually.

  • Sorry.

  • This will be integer so will be the same thing.

  • And listen, we call this ad and then person and right here we will simply take person.

  • Right.

  • And what this allows us to do is to actually generate the Hewitt's ourselves or the actual i d.

  • So you would.

  • And then let's call it I d equals two, and then you it don't and then run them.

  • You it and then simply say return and then insert person passing the I d and then the actual person.

  • Yep.

  • So basically, we have two methods.

  • One that allows us to insert person with a given I D.

  • And you're the one without an i.

  • D.

  • And the idea is simply randomly generated.

  • Now let's go ahead and create a class that will implement this interface.

  • So open up the D A o package and then simply create a class and call it a person and call it fake and then person data access and then service.

  • And then this guy implements and then person d a.

  • O just like that, and then implement the methods and we need to implement insert person.

  • Right, So right here, let's go ahead and define our, um list.

  • So private and then static and then and then list.

  • And this will be a list off person.

  • Simply quality B equals to a new and then ray list just like that.

  • Or in fact, we could even initialize this inside of a static block if we wanted.

  • So now, toe, insert a person all we have to do it simply add to this database right here.

  • So for the sake off this simply go ahead, say d b don't and then add and their new and then person and simply take the actual i d.

  • And then simply say person don't and then get name right, So And finally, let's simply go ahead and return one so we know that it always works, so the session always works.

  • So now let's go ahead and actually defined the actual service.

  • So inside of the service package, go ahead and create new class and then simply call it person and then service.

  • And right here inside of this class, Listen, we have a method to insert a new person, so simply go ahead and say public and then in't and then on and in person.

  • Basically, this will take a person and what we want to do it simply safe return.

  • And right here we need to actually get a reference off the actual person d a.

  • O.

  • So let's go ahead and seem to say private and final and then person Diego and make sure that you get the actual interface and not the concrete class.

  • So don't get the actual fake person data access service because I want to show exactly how to use the penance injection.

  • So then what we need to do is added to the constructor, and now we can simply say person d ay oh, don't and an insert and then person.

  • So remember we had two methods, and if I go back to my interface so person th oh, and in fact, I need to call this the same.

  • So let's be consistent.

  • So person so insert person, actually, so now you can see that the era went away.

  • So basically we have the option off providing the I D or not so in this guy's let's go ahead and have it, you know, randomly generated because we don't care.

  • But if you need to generate the idea yourself, you know that you can do it.

  • So now that we have the service done, let's go ahead and implement the actual FBI.

  • So inside of the A P I package, go ahead and create class and simply call it person and then resource.

  • Or actually, let's go ahead and say controller.

  • So in spring, But world, we tend to name these as controllers, so and then simply say OK, and then let's go ahead and also have a reference to the actual service.

  • So private, final and in person service and then right here we need to add this to the constructor.

  • And then let's simply go ahead another method that adds a person so public and then simply say void.

  • So we don't want to return an integer in this case.

  • So we went toe.

  • So we want to return a status code off 200 or if anything fails, we can promote throw an exception.

  • So or he could also have your custom exceptions if you wish.

  • So let's go ahead and simply say at and then person and this will take a person so person just like that.

  • And then what we need to do is actual safe person service dot and then at the person just like that.

  • So up to this point, we simply have normal Java classes and three units.

  • We know even using spring framework at this point.

  • So let's go ahead and configure everything to start using spring framework.

  • So let's go ahead and open up the actual Diego So fake, um, person data access service.

  • And you see right here inside of the actual service, we have a reference to it.

  • So person Diego and basically we're trying to use the penance injection.

  • So if I was to run this, this would wouldn't work because this person deal is no East, I she ated.

  • And you know, there's no way for this constructed to know about it, right?

  • So this is where we use annotations.

  • To instance, she ate beans.

  • So open up the fake person data access service and the way that we tell spring that this class needs to be east initiated as a bean so that we can inject it in other classes.

  • It's simply by saying act and then repositories, right?

  • So you could also say at and then component.

  • So this is the exact same thing, but depository makes it obvious.

  • that this class is served as a depository.

  • Right?

  • So now that we have a depository, let's go ahead and completed the actual service.

  • So for service, you might take a guess.

  • So this will be at and then service, right?

  • And you could also use at in component, if you wish, but I usually prefer to be specific.

  • So anyone looking at my code know exactly that this is a Savvis.

  • So now that we re much annotated the actual data access service and right here we are injecting, right.

  • So we are injecting into the actual constructor.

  • So the way that we inject it, simply saying at and then auto wired.

  • So basically, we are auto wiring into this interface, right?

  • So now, because we can have multiple implementations off this interface right here, we must have a way to distinguish between them.

  • Right?

  • And this is when you use at and then qualify you right?

  • So, qualifier.

  • And then right here, you can give it a name so fake and then d a o.

  • And basically, I need to go back to my fake person.

  • They access service, and then right here, I'm going to name this as fake and then d a.

  • O.

  • And what this allows me to do is to have multiple implementations.

  • And basically all I need to do is, for example, if I have, for example, a post grass or Mongo D B implementation.

  • I simply say mongo just like that.

  • And then I will have to change anything.

  • So let's go ahead and use the fake d a.

  • O.

  • And now this is why it correctly.

  • So finally, let's go ahead and open up the actual AP I package and then person controller.

  • Let's also go ahead and simply say, act in an order wired and basically Sprinkle injects the actual service into this constructor.

  • And he's gonna learn more about depends injection.

  • Go ahead and check my channel and made a video specific on the palace injection and why it's important for you to know about it.

  • So now remember that I said that this class right here.

  • So the ap, I I go back to our diagram.

  • Is this one right here?

  • And right here we can specify http methods so get post elite and put so these clients right here, right, so they have to issue some requests to our backend.

  • So right now there is no code that tells that this method right here will be served as a post or put or get or the elite.

  • And also, this class is no available as a rest controller.

  • So to do that, we need to use a special annotation and simple say at and then rest controller.

  • Right.

  • So this is a rest control the class that we can have some methods and then expose some endpoints that clients can consume.

  • So in our case, what we want to do is have this meth array here.

  • Toby served as a post request.

  • So basically gets is for retrieving data from your server posts is to adding a resource to your server, put modifying and then the lead for the leading a resource resource from your seven.

  • So too till spring that this method will be used as a post request.

  • We simply used the annotation at and then post and then mapping.

  • And this is all we have to do now.

  • So, obviously, um, we want to add a person from my client, right?

  • So go ahead and download postman.

  • So postman allows you to pretty much just serve as a client because we don't have, for example, a react application or an android app or you know, you we don't have any any any client, right?

  • So we can use a postman as our client, right?

  • So go ahead and download, postman.

  • It's an amazing client.

  • And what we need to do is actually send a person to our server.

  • So before we do that So let me actually clear everything here and basically go ahead and say Post.

  • And then this will be local host Coolum and then 80 80 and enforce slash and then simply, say, a P I fought slash and then V one and then person, right.

  • So after you to find the actual end point, go ahead to the actual body and then simply say roll.

  • And then instead of text, go ahead and send a Jason.

  • So we want to send a Jason pillow to the actual sever.

  • And remember, we have a name.

  • And let's say that this is James and then Bond.

  • So basically, we want to send James Bond to the server.

  • So in order for us to do that, we have to do few things.

  • One is we have to define these properties in our Po Jo so that when we send this Jason Spring knows to take this property and then convert that into a Java class.

  • Right?

  • So in order for us to do that, what we need to do is open up person so inside of the model package, open a person, and then you see right here in the constructor, let's go ahead and define some properties.

  • So our Jason property and this will be the I d and then right here.

  • So if I put this on line at and then Jason property and this will be the actual name, right?

  • So it could also define what properties.

  • But as I said, let's keep this simple so that you understand exactly what's going on.

  • So if I open a postman again, So right here.

  • You see that names of this name right here matches the property that we have in our class person.

  • And if we wanted to send the i d.

  • So it would seem to say I d and then pass, you know, an I d.

  • But we are generating the I d from the server.

  • Therefore we don't need to send it.

  • Now.

  • What I need to do is actually, um, tell that we are receiving this Jason payload from the actual body.

  • So inside off person controller, go ahead and you see right here where you add it, person.

  • So right here simply annotate this with at and then request and then body.

  • Right?

  • So we're simply saying that we want to take the request body and then shovel that inside of this person and then because we have, um so right here.

  • So because we have these this property right here, we will simply tend this Jason object into a person.

  • That's what we're saying.

  • And the final thing that we have to do is actually define this path right here.

  • Right?

  • So right here before rest controller, simply say at and then request and then mopping.

  • And the actual mopping will be a p i V one and then person.

  • So this is all you need to do in order to create your very first endpoint.

  • Wait, spring ball.

  • Now, what we need to do is actually test it.

  • So let's go ahead and simply play the application.

  • So as you can see, Tomcats started on poor 80 80.

  • Now, if I open a postman and then simply send this request right here, you can see that we have a 200 status code and this means that the request went through Fine.

  • Now, obviously, we don't have a way of testing this right because we don't have an end point to retrieve people from our database.

  • So let's go ahead and define the functionality to get people from our database.

  • So let me go ahead and open it in.

  • Tell Ajay and if I collapsed that So go ahead and open up the person D A o.

  • And let's go ahead and say that this will return a list off person and say, Select old people.

  • And then let's go ahead and open up.

  • The A person did access service, so we need to implement that.

  • And then right here, when we need to do is very simple.

  • Return the actual database and open up service.

  • So right here simply create method public and then list off person get and then old people.

  • And then this will return person d ay oh, don't and then select all people.

  • So now we need to define the actual resource or the actual method that will serve as the get request.

  • So simply say, public and then list off and in person and then, uh, get all people.

  • And then what we're going to return is the actual person service don't and then get all people right.

  • So now we need to tell that this method will be served as they get request and the way that you do it.

  • It's simply by saying at and then get and then mapping.

  • So you see that everything has the same pattern at the post mapping get mapping.

  • Now let me go ahead and pretty much just restart the server.

  • There we go.

  • So the actual service started on poor 80 80.

  • So open a postman and then let's actually try and send the actual same request.

  • So we're going to send James Bond.

  • I can see that worked.

  • And now let's go ahead and try and perform a get request to ourselves So the exact same euro send, and there we go so you could see that we have an array with one person that comes from our database.

  • So this is awesome.

  • Now let's go ahead and perform another post.

  • So So right here.

  • So change this to Nelson and then Mandela.

  • You can change this to whatever name you want.

  • To be honest, I'm gonna send that.

  • You can see that's 200 and then simply changed this to Anna.

  • And then Jones send.

  • And now if I perform a get to request to our server, you can see that we have three people.

  • So James Bond, Nelson and Anna Jones.

  • So, as you see, spring ball is just incredible.

  • It's super fun.

  • And as you so like, it didn't take us long to implement this functionality, right?

  • We're just using annotations to drive our implementation, which is awesome.

  • And we can pray much.

  • Deliver results very fast using spring bouquet.

  • Now let's go ahead and implement the other two.

  • Http methods the ability to update a person as well as deleting a person.

  • So open up, you tell a J.

  • If I collapse this and then open up the actual d a o and let's define the actual interface Marshall method.

  • So right here.

  • Simple statement and the elite and then person by D and right here we need to pass the actual idea.

  • So you it and then I d and let's also defined couple things.

  • So let's also define that we want to update a person.

  • So this will return an integer and then simply say update and then person by i d.

  • So right here we're going to pass the actual I d and the actual new person that we want to update, right?

  • So person and also let's define and one more method.

  • And this will be a non optional off and then person and then simply say, select person Bye.

  • And then I d and then simply passed the actual I d inside and there we go.

  • So now let's go ahead and open up the fake person data access service and we have to implement all of those methods so impressing old and then enter and then implement methods.

  • And you can see that we have these three methods that we need to implement okay, and air is gone.

  • So, in fact, let's go ahead and implement select person by 81st.

  • So to implement this method, what we need to do is actually search within our database and see whether we have a person with the given.

  • I D So this one right here, So the way we do it, it's simply by streaming our database.

  • So let's go ahead and use lavish streams, don't and then stream.

  • And you want to learn more about streams.

  • Go ahead and check my video on Java streams.

  • Where cover?

  • Um, this.

  • So let's go.

  • Anything to say filter.

  • And right here we get person, and then they simply say that person dot and then get I d don't equals to I d right.

  • And then what we want is don't and then find and then first.

  • And there we go.

  • So now let's go ahead and open up the actual service and then simply implemented same method here so public.

  • And then this will be an optional off and then person and then get person by I d.

  • And then simply passed the actual I d.

  • And you can see that it's almost the same thing that we're doing right here.

  • Return and then person D'oh!

  • Don't!

  • And then get Ashley select person by I d.

  • And then passed the idea and then open up the controller So person controller and this will be a get so basically we want to public.

  • So this will be a person and then get and then person by i d And then we're going to pass the I d and I'm gonna show you Exactly how do we get that?

  • So I d And then what we want to do is simple safe return, and then person service dot get person by i d lower case I d And then don't or else.

  • And then no.

  • So right here you could actually have a custom message or a custom exception or a custom code thrown to the actual client.

  • So, for example, a 44 with a message saying you were not found, but for this video, let's go ahead and keep it simple and simply return No.

  • Right.

  • So now what we want to do is if I open a postman is you see this I d.

  • Right here.

  • Right.

  • So I want to be able to copy the I d and then simply say Fort slush and then pass the I d.

  • And then when I send the request, I should get that person back.

  • Right?

  • So the way that we get the i d from the path is by simply using an annotation and the annotation is called.

  • So this one is at and then path and then variable and the path variable that we want is an I d.

  • So I d just like that and basically, this is a type right here.

  • So now let's go ahead and to find the actual mopping.

  • So this will be act and then get and then my pin And now because we want to have the actual i d in the path so inside, simply say path and then what you want is double quotes and then curly brackets and then pass I d.

  • So basically, this will allow youto have fort slash and then some i d.

  • And then right here you're simply grabbing the i d and then turning that into a u it right.

  • So let's go ahead and test this.

  • So if I pretty much just restart the actual server, there we go.

  • I've been running.

  • So if I open a postman now we need to issue some requests so you can see that you know, every time that we will start the server, we lose everything from our list.

  • That's a disadvantage of having an in memory database.

  • But for this video you can see exactly how everything works.

  • So let's go ahead and sends a post request there.

  • And then let's get everyone so removed the actual you it there.

  • So send.

  • So we have Nelson right there.

  • And basically, now we can grab Nelson's, um, I D and then do 1/4 slash and then paste that right.

  • So now if I send, you can see that we have only one object, right?

  • It's so it's no an array, but it's an object.

  • And if also change thing, I d for something that doesn't exist.

  • Send.

  • You can see that.

  • Actually, it's a bad request because the you it it's completely wrong.

  • So it's no valid.

  • You eat that I've just sent to the server, but you get the idea.

  • So if I go ahead and let's go ahead and add, um, Jones.

  • So send.

  • And then let's get everyone and let's grab on a Jones I d.

  • Right there and then fourth slash send that you can see that works right?

  • And in fact, let me go ahead and say you it generator and let me generate you it.

  • So I want one you were here.

  • And if I grab this and then go back to postmen and then perform a get request with that you were there.

  • So we find a lead, everything and then paste that you it and then send you see that it's a 200.

  • But we have nothing in our database without you it, right?

  • So as I said, So you should be throwing a 44 with a custom exception.

  • But in this video, you can see exactly that.

  • We're returning Nothing, because there is no one with that.

  • You it against our database.

  • So let's go ahead and implement the remaining methods.

  • So if I collapse this so let's go ahead and implement everything in one.

  • Go now.

  • So open up the actual fake person that acts of service and to delete a person.

  • What we need to do is simply get the person.

  • If the person is there, the leader otherwise return zero.

  • So let's go ahead and simply, uh, say select and then person by I d.

  • We're gonna grab the I d.

  • And remember, this returns an optional so person and then maybe and right here is simple.

  • See if and then person, maybe dot and then is empty.

  • So if it is empty, we want to return zero other wives.

  • What we want to do is actually remove from the database, right?

  • So if it's there, we want to remove so d b dot and then we move.

  • And then person, maybe don't and then get just like that and then return one.

  • Right?

  • So we know that we did the lead.

  • This person.

  • Now let's go ahead.

  • An implement update, person.

  • So what we're going to do, it's simply say return and then get or actually select person by i d.

  • We're going to pass the I d.

  • And then what we want to do is say dot and then map.

  • So right here we get a person.

  • Let's call it p So pee.

  • And as I said, if you want to learn about, you know, jobless dreams, maps, operations filtering, go ahead and check my YouTube video where I teach all of this.

  • So now let's go ahead and try and get the actual index off this person so int and then index off and then person to delete equals two and then d B don't and an index off and then passed the actual person.

  • Right.

  • So now we have an index.

  • So I'm gonna say, if the actual index to delete and I'm missing a need right there.

  • So if the index to the lead is greater or equal to zero, what we want to do is actually set the actual person.

  • So it means that we found someone, right?

  • So let's go ahead and say D b don't and then set and then index off the person to delete and then simply passed the actual person right?

  • And now what?

  • We can do it simply return one right here.

  • So for the wise, so else we can simply say return and then zero and then right here.

  • So because we need to return an integer, simply say Don't and then or else and then zero, right?

  • So basically, if I formed this So basically, I'm saying select the person and then map that person to something else, right?

  • And if the actual index off that person is greater or equal to zero, we know that we found that person and then set the contents off that person to the new person that we've just received from the client and then returned one.

  • Everything is fine.

  • Otherwise return zero or if select person by the is not present, we don't do anything and simply return zero.

  • So this is what it means.

  • So now let's go ahead and simply, you know, feed this through the service as well as controller and then test it.

  • So let's go ahead and simply open up passing service.

  • So let's go ahead and say public and then ain't the elite and then person and then right here we're going to take the actual I d.

  • So I d.

  • And then this will be very simple.

  • All we need to do is simply say return and then person D'oh thought and then delete by I d passed the actual i d and we're done to up their person.

  • Sorry.

  • So if I closed that Soto up their person, what we need to do is simply say public and then ent update and in person.

  • So we're going to take the actual i d.

  • So you it and then I d.

  • And then this person is the new person so new and then person, right?

  • And then what we need to do is simply sail return person d 00 don't And an update And then I d right so you can see that I don't have much logic going on here.

  • And this is because, you know, I'm just teaching you exactly how to use amino spring but toe to create a B I essentially, you know, inside of the service you have whatever logic your application requires, right?

  • So I'm just making it simple for you to understand.

  • Exactly.

  • So you don't have a lot of code going on.

  • Basically, you see the entire process off what we're doing.

  • So now let me go back to the actual person controller and simply expose those to a rest a b I.

  • So the 1st 1 is update or actually delete.

  • So let's go ahead and say the lead so public and then void and then delete.

  • And then a person by D, we simply passed the i d.

  • And pretty much the same thing at and then path variable.

  • And then this will be the I d.

  • And then we want that to be you.

  • It i d and then return.

  • Or actually, there's no need to return here because this is void.

  • So simply say person service dot and then the lead person past your i d.

  • Now this has to be guess what at and then remember, we're the leading a resource from the server, so delete and then mapping.

  • So now inside, we have to have the path and then the actual I d.

  • Right.

  • So this i d is this one right here which we can pass down to the service.

  • So let's also go ahead and implement the actual update So the update will be at and then put and then mapping and public and then void and then update person.

  • And then this will take a person from the request body.

  • So at and then request body, we'll take it person to update.

  • There we go.

  • And now simply say person service don't and an update.

  • And in fact, we also need the actual path.

  • So the path we'll include the actual i d.

  • So I d.

  • And let's go ahead and say that at and then path and invariable and that will contain the i d.

  • And this will be a u it and then call the I d.

  • And there we go.

  • So Now we can pass I d and then person to update right.

  • And to be honest, this is all we have to do to implement our rest a p I.

  • So let's go ahead and test it.

  • So I'm gonna pretty much just restart the server.

  • So let me go ahead and open a postman.

  • So let's start.

  • We'd one person.

  • So let me send this request and then let me get everyone so send you see that we have Nelson Mandela.

  • So let's go ahead and change Nelson's name to something else.

  • So go ahead and simply create a new request.

  • So go ahead and change this to a put and the body.

  • So instead of saying and Nelson Mandela, they simply say that this is Alex and in Korean, right?

  • And now let's go ahead and simply pass Nelson's Mandela right here and basically we just changing his name.

  • So if I go ahead and send, you can see that the request did work.

  • And now if I get everyone from our database, so if I perform a get request and then send, you can see that that didn't work and let's go ahead and deeper this in a second.

  • But for now, let's go ahead and pretty much try and delete this person.

  • So, Nelson Mandela.

  • So if I put forth slash and then the actual I d and then delete and then send we got 200.

  • If we get everyone, you see that Nelson is not there and it's been deleted.

  • So we had a bag.

  • So if I post, let's go ahead and post on Jones.

  • And basically we couldn't update the actual name.

  • So if I get on a Jones, get the actual I d and then perform a put.

  • And this will be Anna Jones, I d.

  • Just like that.

  • And then Bodie and then less simply say update and then update send still a 200.

  • But if we get everyone, you see that still on the job.

  • So we have a bug.

  • So let's go ahead and try and find out why exactly this is happening.

  • So let me go back to intelligent they and then close this and then open up the actual their access service.

  • And now this is the actual method.

  • So we are selecting the person mapping and then so basically so right here we have index off person to delete, so this would be actual update.

  • So to update and d b index off person, So basically, yes.

  • So this is the air.

  • So so right here.

  • So you see that we have So this person right here, So this is what we want to update.

  • So right, So let me rename this to update.

  • Right.

  • So the index off person toe update, you know, it's minus one, so this will always return zero.

  • So what we want is actual pee.

  • So P is the actual person.

  • And in fact, as I mean, rename this two person so we want this person that we found so person.

  • And then this should return the actual index.

  • And then if it's bigger or equal to zero, what we want is new.

  • So because this update doesn't contain the actual i d.

  • So we need new person and then passed the I d and then simple see up they dot and then get name.

  • So yeah, I think this makes more sense.

  • So let's go ahead and we start the server.

  • Very go.

  • So now if I open a postman and then pretty much just get so let's start from scratch.

  • So no one in the database, let's go ahead and post Onda Jones.

  • And now we have to get let's go ahead and get so we get the actually, you it off, Jones.

  • And then right here, let's go ahead and performing Put to this, um, en pointe and simply passed on Jones, you it or I d And then in the actual body less also changed this to Anna and then Montana just like that.

  • And then if we sent so we go 200 now this should work.

  • So go ahead and send.

  • And that s you can see that this now works.

  • So you see that we pretty much just managed to implement all of the methods the find in our a p I.

  • So it is nice to have, like, tests for everything right here.

  • And basically I wrote tests for this'll class.

  • So go ahead and download the report down below and you can run all the tests and see that this works as expected.

  • So I've got unit tests around all of this.

  • So one last thing that I want to mention is that you see inside of this person controller right here.

  • So this person control that we can have, um, you know, some meditations that enforce no ability in our application.

  • So we can pretty much just use someone so meditations and enforce that.

  • This object right here, it's all this present and also valid.

  • So go ahead and open a person, um, model.

  • And then right here, what we can do is actually say that this field right here.

  • So the actual name, This cannot be blank.

  • So at and then not and then blank.

  • Right.

  • So the reason why I'm not saying not know is because if you have an empty string, it's not.

  • No, but if you have Ah, um, an inter string, it's actually blank.

  • So basically, we don't want that Toby blank.

  • Right?

  • So then go back to the actual controller.

  • And now you can say that the request body.

  • So this has to be so at and then valid, right?

  • And then we can also say that this must not be no.

  • So we could also do the same here so we can enforce that this is valid and also at and then not know, right.

  • So now if I permit, restart the server, so you see exactly what we're doing.

  • So basically, we won't fail as soon as possible instead of having to deal with a nail ability inside of our code.

  • So now let's go ahead and open a postman, and then let's go ahead and pretty much try and post someone to our database.

  • So right here.

  • So you see that we have and Jones.

  • But if we send, for example blank so send, you see that we get an error.

  • And basically this is working.

  • I was expecting as expected.

  • So you see that it's a bad request, and it's pretty much saying that validation failed for object person right here.

  • So this is awesome.

  • And you can see the actual deform message which says that the field name must not be blank.

  • So now if I go ahead and add the correct payload, so right here.

  • So let's go ahead and say John and then Jones and then send You can see that we have way too underside us code.

  • And if I go ahead and get everyone and see that we have John Jones.

  • So this is it, to be honest.

  • So now he should be familiar with spring boot and writing ap eyes for your applications.

  • So now the cool thing about it is that you can have a client, a p i, and then do all kind of things depending on your application.

  • So one more thing, actually, then I forgot to show you is dependency injection, right.

  • So let's say that we want to have a post press implementation, right?

  • Another database or connecting to a different database. 01:02:21.890 --> 01:02:27.

Hi.

Subtitles and vocabulary

Click the word to look it up Click the word to find further inforamtion about it