Getting Data From DB in Our Rest Controller

Develop RESTful Java Web Services Using Spring Boot Get Started with REST and Spring Boot
7 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$49.99
List Price:  $69.99
You save:  $20
€46.53
List Price:  €65.14
You save:  €18.61
£39.80
List Price:  £55.73
You save:  £15.92
CA$68.29
List Price:  CA$95.61
You save:  CA$27.32
A$75.92
List Price:  A$106.30
You save:  A$30.37
S$67.59
List Price:  S$94.64
You save:  S$27.04
HK$390.62
List Price:  HK$546.91
You save:  HK$156.28
CHF 45.35
List Price:  CHF 63.50
You save:  CHF 18.14
NOK kr546.18
List Price:  NOK kr764.69
You save:  NOK kr218.51
DKK kr347.06
List Price:  DKK kr485.92
You save:  DKK kr138.85
NZ$83.56
List Price:  NZ$117
You save:  NZ$33.43
د.إ183.60
List Price:  د.إ257.06
You save:  د.إ73.45
৳5,472.53
List Price:  ৳7,661.98
You save:  ৳2,189.45
₹4,170.94
List Price:  ₹5,839.65
You save:  ₹1,668.71
RM236.95
List Price:  RM331.75
You save:  RM94.80
₦61,737.65
List Price:  ₦86,437.65
You save:  ₦24,700
₨13,922.21
List Price:  ₨19,492.21
You save:  ₨5,570
฿1,839.58
List Price:  ฿2,575.56
You save:  ฿735.98
₺1,615.93
List Price:  ₺2,262.43
You save:  ₺646.50
B$255.52
List Price:  B$357.76
You save:  B$102.23
R925.67
List Price:  R1,296.01
You save:  R370.34
Лв90.98
List Price:  Лв127.38
You save:  Лв36.40
₩67,934.14
List Price:  ₩95,113.23
You save:  ₩27,179.09
₪185.78
List Price:  ₪260.11
You save:  ₪74.32
₱2,856.70
List Price:  ₱3,999.61
You save:  ₱1,142.91
¥7,653.44
List Price:  ¥10,715.43
You save:  ¥3,061.99
MX$846.70
List Price:  MX$1,185.45
You save:  MX$338.74
QR181.98
List Price:  QR254.79
You save:  QR72.81
P682.59
List Price:  P955.69
You save:  P273.09
KSh6,733.65
List Price:  KSh9,427.65
You save:  KSh2,694
E£2,396.77
List Price:  E£3,355.67
You save:  E£958.90
ብር2,849.43
List Price:  ብር3,989.43
You save:  ብር1,140
Kz41,866.62
List Price:  Kz58,616.62
You save:  Kz16,750
CLP$47,373.02
List Price:  CLP$66,326.02
You save:  CLP$18,953
CN¥361.77
List Price:  CN¥506.51
You save:  CN¥144.74
RD$2,892.40
List Price:  RD$4,049.59
You save:  RD$1,157.19
DA6,728.32
List Price:  DA9,420.19
You save:  DA2,691.86
FJ$112.64
List Price:  FJ$157.70
You save:  FJ$45.06
Q387.56
List Price:  Q542.62
You save:  Q155.05
GY$10,437.32
List Price:  GY$14,613.08
You save:  GY$4,175.76
ISK kr6,994.10
List Price:  ISK kr9,792.30
You save:  ISK kr2,798.20
DH504.29
List Price:  DH706.05
You save:  DH201.75
L885.56
List Price:  L1,239.86
You save:  L354.29
ден2,864.78
List Price:  ден4,010.92
You save:  ден1,146.14
MOP$401.51
List Price:  MOP$562.15
You save:  MOP$160.63
N$930.33
List Price:  N$1,302.54
You save:  N$372.20
C$1,836.63
List Price:  C$2,571.43
You save:  C$734.80
रु6,655.03
List Price:  रु9,317.58
You save:  रु2,662.54
S/187.71
List Price:  S/262.81
You save:  S/75.10
K191.79
List Price:  K268.53
You save:  K76.73
SAR187.49
List Price:  SAR262.51
You save:  SAR75.01
ZK1,342.57
List Price:  ZK1,879.71
You save:  ZK537.13
L231.55
List Price:  L324.19
You save:  L92.63
Kč1,163.97
List Price:  Kč1,629.65
You save:  Kč465.68
Ft18,122.66
List Price:  Ft25,373.17
You save:  Ft7,250.51
SEK kr541.93
List Price:  SEK kr758.75
You save:  SEK kr216.81
ARS$43,903.87
List Price:  ARS$61,468.94
You save:  ARS$17,565.06
Bs344.52
List Price:  Bs482.36
You save:  Bs137.83
COP$194,950.93
List Price:  COP$272,946.91
You save:  COP$77,995.97
₡25,444.18
List Price:  ₡35,623.88
You save:  ₡10,179.70
L1,237.75
List Price:  L1,732.95
You save:  L495.20
₲373,658.53
List Price:  ₲523,151.84
You save:  ₲149,493.31
$U1,916.38
List Price:  $U2,683.09
You save:  $U766.70
zł201.31
List Price:  zł281.85
You save:  zł80.54
Already have an account? Log In

Transcript

Now that we have done the mapping of customer class to Customers table in the RDBMS, let's now create an interface that can give us all the values from the table. Now, this is where springs magic comes into picture, all we have to do is to create an interface that extends something called crud repository. And automatically spring is going to create a class at the current time on the fly and gives an object of the same. Let's understand this by a practical example. Now, in our controller, we needed to return the list of all customers. So for that, we have to get the data from the database.

While we have done the mapping over here. The question is, how do I get the data from the database. Now traditionally, you might have used JDBC to write SELECT statement, get the data from the database, and so on and so forth. Or if you have used hibernate or JPA Then you may want to use the session or entity manager to get the data and work with that. But in all cases, you have to write the code. But in this case, let's say we don't have to write any code and still get the data from the database.

Let's understand this by creating an interface called customer repository. And let's change the package from controllers to do or repository as you feel. And then importantly, we have to extend this with crud repository. So let's say okay, and then come out of this. Now, you can see here that there is an error because this is an incomplete code. So we say that this is a customer repository, extending crud repository.

Now crud is an acronym for create read, update, delete. So this particular interface has Already functions related to inserting the data retrieving the data updating or deleting the corresponding data. So we have to specify what kind of entity object that it works with. So t here is the type. So I'm going to say customer. And then ID is the data type of the primary key value of this.

Now if we go back to customer, you can see that the ID annotation has been added to integer. So this is the data type of our ID. So I go back to my repository, and I say that the data type is integer. And that's all I have to do. So if you click on this crud repository and press function key four, you will see that there are a bunch of functions over here for example, count gives me the number of customer objects that are available in the database, delete, deleting an object, Delete on of course, all of the records would be deleted. There is also find all exist By ID delete by ID save, which is meant for inserting, as well as updating, depending on whether the primary key is null or there is a value.

So all these functions are already available in our interface. One last thing I have to do here is to mark this with at the rate repository. This way, spring knows that whenever we want an object of this class, it will create a class and then instantiate and give that same thing to us. Now how can I use this for now? Now this is where we go back to our customer rest controller. And we say that, hey, we need an object of customer repository.

So let's call it as a repo. We'll also make them as private. Now notice that this is a member variable of this class, so by default it is now so we want to use this repo and then return an object. So we're going to remove this C one and C two. Obviously arrays.as list, I'll simply have to say report dot find all. Now you can see that the Find all is an iterable.

And iterable is a super type for list, so we can't really do a return. Now there are two ways to handle this scenario number one, I can do a casting, or I can change this from list to to iterable. Now there is no problem. I can work with this. But there is only one issue here. Since this is a member variable, the default value for this is null.

Obviously, null dot final is going to be a null pointer exception. This is where we have to tell spring that, hey, when this function is being called, we are actually needing an object of repository, which is a customer repository. And the customer repository is an interface. So we want an object of the data type. And all I have to do is to tell spring that hey, I need an object of customer repository. And how do we do this, we simply say at the ridge, auto wire Now when you say auto word spring knows that this is a subtype of crud repository.

And spring knows a way of creating a class because we have given what is the name of the class like customer, what is the primary key value, and everything automatically is done. A class is created on the fly and an object is also created on the fly. That object is given to me here, so it is no longer null. And it is since it is not null, it's going to go and get the data from the database. So let's go and organize imports by pressing Ctrl Shift o on Windows command shift on Mac, so everything is intact. Let's run our application and see if we get the desired output.

We got one error here that is cannot load driver class or dot h store our driver. That's because we said in our Application dot properties that the driver class happens to be this one, and it's not there in any of the jars. Now, we could handle this in two ways. Number one, you can go back to spring initializer and then specify that we want to look for hitch to database and then get the pom dot XML etc, etc. But ultimately, you can add the jar directly from the h2 jar that you downloaded earlier. So I go to project properties, and then select Java build path libraries, add external jars.

And then I go to my home directory where I have the hitch to bin and there we have hitch two dash one dot four dot 197 dot jar open and then say Apply and close. let's rerun the application dot java once again and see if we got any more errors looks like everything is okay. Let's go back to our localhost double seven double seven slash API slash customers. Let's refresh and we should see thousand records over here.

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.