Add View All Posts Functionality

Spring Boot: Build Your Website Real Quick Building Restful Micro-Service With Spring Boot
11 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$99.99
List Price:  $139.99
You save:  $40
€96.21
List Price:  €134.70
You save:  €38.49
£79.93
List Price:  £111.90
You save:  £31.97
CA$143.79
List Price:  CA$201.32
You save:  CA$57.52
A$160.48
List Price:  A$224.69
You save:  A$64.20
S$135.83
List Price:  S$190.17
You save:  S$54.33
HK$777.07
List Price:  HK$1,087.93
You save:  HK$310.86
CHF 89.45
List Price:  CHF 125.24
You save:  CHF 35.78
NOK kr1,141.95
List Price:  NOK kr1,598.78
You save:  NOK kr456.82
DKK kr717.64
List Price:  DKK kr1,004.72
You save:  DKK kr287.08
NZ$177.52
List Price:  NZ$248.53
You save:  NZ$71.01
د.إ367.25
List Price:  د.إ514.17
You save:  د.إ146.91
৳11,994.74
List Price:  ৳16,793.12
You save:  ৳4,798.37
₹8,496.63
List Price:  ₹11,895.62
You save:  ₹3,398.99
RM450.75
List Price:  RM631.07
You save:  RM180.32
₦155,628.43
List Price:  ₦217,886.03
You save:  ₦62,257.60
₨27,934.96
List Price:  ₨39,110.06
You save:  ₨11,175.10
฿3,444.45
List Price:  ฿4,822.37
You save:  ฿1,377.92
₺3,518.54
List Price:  ₺4,926.10
You save:  ₺1,407.55
B$617.53
List Price:  B$864.57
You save:  B$247.04
R1,837.80
List Price:  R2,572.99
You save:  R735.19
Лв188.31
List Price:  Лв263.64
You save:  Лв75.33
₩144,872.25
List Price:  ₩202,826.95
You save:  ₩57,954.69
₪364.89
List Price:  ₪510.86
You save:  ₪145.97
₱5,883.01
List Price:  ₱8,236.45
You save:  ₱2,353.44
¥15,672.80
List Price:  ¥21,942.55
You save:  ¥6,269.75
MX$2,028.91
List Price:  MX$2,840.56
You save:  MX$811.64
QR366.34
List Price:  QR512.89
You save:  QR146.55
P1,382.59
List Price:  P1,935.69
You save:  P553.09
KSh12,923.70
List Price:  KSh18,093.70
You save:  KSh5,170
E£5,091.27
List Price:  E£7,127.98
You save:  E£2,036.71
ብር12,764.56
List Price:  ብር17,870.89
You save:  ብር5,106.33
Kz91,790.82
List Price:  Kz128,510.82
You save:  Kz36,720
CLP$99,155.08
List Price:  CLP$138,821.08
You save:  CLP$39,666
CN¥729.81
List Price:  CN¥1,021.77
You save:  CN¥291.95
RD$6,104.52
List Price:  RD$8,546.58
You save:  RD$2,442.05
DA13,454.60
List Price:  DA18,836.98
You save:  DA5,382.37
FJ$232.11
List Price:  FJ$324.96
You save:  FJ$92.85
Q773.21
List Price:  Q1,082.52
You save:  Q309.31
GY$21,000.46
List Price:  GY$29,401.48
You save:  GY$8,401.02
ISK kr13,903.60
List Price:  ISK kr19,465.60
You save:  ISK kr5,562
DH1,007.40
List Price:  DH1,410.40
You save:  DH403
L1,841.78
List Price:  L2,578.57
You save:  L736.78
ден5,922.50
List Price:  ден8,291.74
You save:  ден2,369.23
MOP$803.42
List Price:  MOP$1,124.82
You save:  MOP$321.40
N$1,834.70
List Price:  N$2,568.66
You save:  N$733.95
C$3,694.32
List Price:  C$5,172.20
You save:  C$1,477.87
रु13,665.58
List Price:  रु19,132.35
You save:  रु5,466.77
S/374.71
List Price:  S/524.60
You save:  S/149.89
K406.86
List Price:  K569.63
You save:  K162.76
SAR375.72
List Price:  SAR526.02
You save:  SAR150.30
ZK2,777.96
List Price:  ZK3,889.25
You save:  ZK1,111.29
L478.81
List Price:  L670.35
You save:  L191.54
Kč2,418.25
List Price:  Kč3,385.65
You save:  Kč967.40
Ft39,800.47
List Price:  Ft55,722.25
You save:  Ft15,921.78
SEK kr1,103.66
List Price:  SEK kr1,545.17
You save:  SEK kr441.50
ARS$102,190.76
List Price:  ARS$143,071.15
You save:  ARS$40,880.39
Bs693.61
List Price:  Bs971.08
You save:  Bs277.47
COP$437,799.12
List Price:  COP$612,936.28
You save:  COP$175,137.16
₡50,480.33
List Price:  ₡70,674.48
You save:  ₡20,194.15
L2,548.03
List Price:  L3,567.35
You save:  L1,019.31
₲784,126.06
List Price:  ₲1,097,807.85
You save:  ₲313,681.79
$U4,460.41
List Price:  $U6,244.75
You save:  $U1,784.34
zł409.96
List Price:  zł573.96
You save:  zł164
Already have an account? Log In

Transcript

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

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.