Hey there, welcome back friends. In our last videos we have checked like multiple things related to a Spring Boot application. We have already checked a very simple RESTful web application again, which is opened in front of us and this is again a good thing we were able to create this API. Now apart from this we have already discussed couple of more things which are background related things. For example, how important is to follow the rules when it comes to restful application, it is very important to follow proper convention proper standardization right. Apart from that, we have already checked in our last video that what are the different ways of creating a Spring Boot application so we know the background we have checked like couple have things related to controller rest controller, web application, RESTful API application and things like that.
Now is the time to start building an actual micro service. So let's start doing it. So in order to save some time, I have already added this project and we have this particular file with us. First of all, we will give the name to this file as blog blog is what a blog is nothing but the application. So we will rename this and we will follow convention to give a proper name to a major file the file which will run the application so we will give the name as a blog. Let me make the changes.
Let me rename the file as blog. And let me also update the servlet initializer. All right. Now moving forward, let us quickly create a controller so I will right click navigate to new and let us create a new class. And this controller would be based on our resource and pay On our discussion in our last videos, our resource would be post in a blog website, post artha resource post are the articles which we will release on the blog website right. So, our resource as post, so, we will make use of the plural form of the name post.
So, the name would be like posts, but these are small little things are very important again and the package would be ordered studies you taught controller. Now apart from this name post, this post is what this is a controller so let us also add in the name as controller in here so it will be like posts controller and we will click on Finish. Alright, now friends, as of now we will deal with a list of what we say post but we won't connect with the database as of now we will do that later down the line as a result. First of all, we would be creating a list Post which would be the working data for us for this project. So, let us create a variable and this would be a list of post. Now, what is this post this post would be an entity All right.
So, first of all before creating this last we must create an entity again right. So, this is yet another step. So, again I will highlight this package and we will create a new class. Now, what is an entity entity is nothing but a representation of a table in the database. So, in here we will create a package first of all by the name of org dot steady easy dot entity and we will create a class now the name of the class would be singular form of the resource which is post and we will click on Finish alright. And now let us add in the properties and these property will symbolize the columns in database and we will definitely discuss about database connectivity in our forthcoming videos.
So, the first entity would be the post ID. So, it would be like post ID, then we will create a yet another property and this would be a string entity and this would be for example title and then we will create a yet another entity by the name of body All right. So, this looks good. And let us make the changes and follow the conventions All right. Now moving forward, first of all, let us create a constructor for this class. So, we will navigate to the source and create a constructor using field All right, and also let us get rid of this call to the super method.
Let me organize the class a little and now let us also quickly create the getters and setters. And finally, let us also create the to string method. All right, so this is cool is that how easily we are able to create this class and most of the code is coded by Eclipse itself. And this is nothing but a plain old Java object, a pojo class. All right, now let's move into the controller. And now let us create a sample data set.
So it would be like a list of posts and we will give the name as plural form of the resource which is posts equals new. Now the convention which I am following is the convention which I follow, right. So based on your, what we say architecture of the application, you may follow different conventions. That's up to you but follow the convention based on the architecture of your application, which is very important. Let us quickly add in the packages. All right, and now I will again right click, navigate to source and we will create a constructor and this would be a default constructor and inside this default constructor We will initialize this list.
So, in here we will type in posts dot add and let us add in a new post. Let us give the ideas one title as for example, data type and body as for example, sample text, we are not worrying about the actual content inside these objects, we will simply add some sample data to work of it and we will definitely perform the crude operation based on this sample list as well. Alright, so, let me update couple of things. All right. So, this looks good this is simply a sample data and once we are done with this sample object on which we will do this operations, let us create a specific method which will provide us the list of all the posts, which are available inside this list. Now based on the convention, based on our diagram from the previous video, let us create a method, this would be a public method return type would be a list of posts, and the name would be get posts.
And that's it all right. And let us also return the list here we go all right. So now finally, let us add in the annotations first of all this controller is nothing but a rest controller. So let us add in the notation of rest controller. And now let us also add in the notation of request mapping. And mapping would be yes, the mapping would be slash posts.
All right, so this looks good to me. And finally, later quickly run this blog application. Here we go. Let me show you the console. Alright, so the application is started. cool enough.
Now let us quickly navigate to Google Chrome. And we will navigate to localhost colon 8080. Here is the post. But if I navigate to slash posts, then definitely we are able to see a JSON response. And this is amazing how a list is getting converted to a JSON response. Cool, right?
So this is amazing that by default, our Spring Boot will convert the list into a JSON response amazing difference that is a flock. Whenever we are like creating a Spring Boot application, there is a design pattern which is followed and that is known as creating services. So In here, we must create service layer and creating a service layer is a good practice that will definitely decrease the level of complexity and that will give you a high level of abstraction alright. So, in here, let us quickly navigate to our structure of the application and in here we will be creating a new class and the name of the class will be posts service All right, and we will give the package as or daughter study z dot services and we will click on finish all right and first of all we will mark this class as a service.
So, now our Spring Boot application will treat this class as a service and now definitely in here we will create a method. This would be a public method this will return a list of post name would be get posts, and the thing the logical part of our controller is doing will be passed to our service layer. So in here, first of all, let me do a small little change. Let me cut this particular part and paste it in here. That's one thing. And definitely we need to change the name to post service.
This is a constructor, right? So let me quickly make the change in here. Alright, so this is good enough, and we will return the list in here. So it would be like return posts. And this is good. This is nice.
And now inside of our controller, we will do what in here, we will simply call in our service. So this would be like new post service, dot get posts. And that's it. Now, by making these small little changes, the functionality of our application have no impact. We can simply relaunch this application, and we can revisit our page and hit Enter. And we will get the same response.
All right, so this is cool. This is nice. And this is it for today's video. I hope you guys enjoyed this video. Thanks for watching. Have a nice day and take care