16. Consuming Restful Service

25 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€64.99
List Price:  €92.84
You save:  €27.85
£55.80
List Price:  £79.72
You save:  £23.91
CA$95.74
List Price:  CA$136.78
You save:  CA$41.04
A$105.97
List Price:  A$151.39
You save:  A$45.42
S$94.71
List Price:  S$135.31
You save:  S$40.59
HK$547.38
List Price:  HK$782
You save:  HK$234.62
CHF 63.50
List Price:  CHF 90.72
You save:  CHF 27.21
NOK kr760.18
List Price:  NOK kr1,086.02
You save:  NOK kr325.83
DKK kr484.74
List Price:  DKK kr692.51
You save:  DKK kr207.77
NZ$116.49
List Price:  NZ$166.43
You save:  NZ$49.93
د.إ257.06
List Price:  د.إ367.25
You save:  د.إ110.18
৳7,679.15
List Price:  ৳10,970.69
You save:  ৳3,291.53
₹5,844.24
List Price:  ₹8,349.28
You save:  ₹2,505.03
RM331.61
List Price:  RM473.75
You save:  RM142.14
₦86,437.65
List Price:  ₦123,487.65
You save:  ₦37,050
₨19,466.20
List Price:  ₨27,810.05
You save:  ₨8,343.85
฿2,579.91
List Price:  ฿3,685.75
You save:  ฿1,105.83
₺2,258.19
List Price:  ₺3,226.13
You save:  ₺967.93
B$355.28
List Price:  B$507.56
You save:  B$152.28
R1,291.06
List Price:  R1,844.45
You save:  R553.39
Лв127.20
List Price:  Лв181.73
You save:  Лв54.52
₩95,163.27
List Price:  ₩135,953.36
You save:  ₩40,790.08
₪260.34
List Price:  ₪371.93
You save:  ₪111.59
₱4,006.12
List Price:  ₱5,723.27
You save:  ₱1,717.15
¥10,811.89
List Price:  ¥15,446.23
You save:  ¥4,634.33
MX$1,180.56
List Price:  MX$1,686.59
You save:  MX$506.03
QR255.22
List Price:  QR364.61
You save:  QR109.39
P950.05
List Price:  P1,357.27
You save:  P407.22
KSh9,308.67
List Price:  KSh13,298.67
You save:  KSh3,990
E£3,339.92
List Price:  E£4,771.52
You save:  E£1,431.60
ብር4,017.22
List Price:  ብር5,739.13
You save:  ብር1,721.91
Kz58,559.69
List Price:  Kz83,660.29
You save:  Kz25,100.60
CLP$65,083
List Price:  CLP$92,979.70
You save:  CLP$27,896.70
CN¥496.09
List Price:  CN¥708.73
You save:  CN¥212.64
RD$4,059.13
List Price:  RD$5,799
You save:  RD$1,739.87
DA9,404.13
List Price:  DA13,435.05
You save:  DA4,030.92
FJ$157.14
List Price:  FJ$224.49
You save:  FJ$67.35
Q543.86
List Price:  Q776.98
You save:  Q233.11
GY$14,638.84
List Price:  GY$20,913.53
You save:  GY$6,274.68
ISK kr9,768.50
List Price:  ISK kr13,955.60
You save:  ISK kr4,187.10
DH701.39
List Price:  DH1,002.03
You save:  DH300.64
L1,239.86
List Price:  L1,771.31
You save:  L531.44
ден4,006.46
List Price:  ден5,723.76
You save:  ден1,717.29
MOP$563.24
List Price:  MOP$804.66
You save:  MOP$241.42
N$1,288.69
List Price:  N$1,841.06
You save:  N$552.37
C$2,575.52
List Price:  C$3,679.48
You save:  C$1,103.95
रु9,351.66
List Price:  रु13,360.08
You save:  रु4,008.42
S/260.79
List Price:  S/372.58
You save:  S/111.78
K270.67
List Price:  K386.69
You save:  K116.01
SAR262.50
List Price:  SAR375.01
You save:  SAR112.51
ZK1,901.47
List Price:  ZK2,716.50
You save:  ZK815.03
L323.43
List Price:  L462.07
You save:  L138.63
Kč1,625.98
List Price:  Kč2,322.93
You save:  Kč696.95
Ft25,280.20
List Price:  Ft36,116.11
You save:  Ft10,835.91
SEK kr759.20
List Price:  SEK kr1,084.61
You save:  SEK kr325.41
ARS$61,608.98
List Price:  ARS$88,016.60
You save:  ARS$26,407.62
Bs483.53
List Price:  Bs690.78
You save:  Bs207.25
COP$273,394.26
List Price:  COP$390,579.97
You save:  COP$117,185.71
₡35,797.53
List Price:  ₡51,141.53
You save:  ₡15,343.99
L1,728.55
List Price:  L2,469.47
You save:  L740.91
₲523,227.64
List Price:  ₲747,500.10
You save:  ₲224,272.45
$U2,673.53
List Price:  $U3,819.50
You save:  $U1,145.96
zł280.44
List Price:  zł400.65
You save:  zł120.20
Already have an account? Log In

Transcript

Session, let us learn how to consume a spring restful service. For this, we are going to create a simple spring MVC application. From the MVC application, we are going to connect to the RESTful web service to consume the methods that are available over there. I am given a simple example. For consuming a RESTful web service, you need to use rest template class. With the rest template class there are few methods available with a helper method.

You can access a list with service and consume the methods when you are invoking a rest web service in that case, you are client application and the rest application should be running in the server. Now, let me show you the methods that are available in this template class. Get four objects Change head forehead does get for entity delete put pose for object pose for location and pose for entity. We will be learning how to use this get for object and force for object. Let me go back to eclipse and we will start I have created a spring MVC application using spring starter project, I have added just Weber c dependency, we are going to consume the rest web service that we have created in the previous session. This is just a blank, it doesn't need any jars later to the database.

It is trying to invoke another application which is connecting to the database that is you are trying to connect to a rest web service which is performing CRUD operations with the database. Let us see the flow of this application. Here I've got as usual, the application class then I have got a contract In this case, I have given the package name is comm dot client app just to specify that this is a client application This is only for understanding if you want to get even habitus combat vuca inside this controller I have created a client controller, this is a class the client controller is annotated with at the rate controller FA told you of the success simple spring MVC application it is not a rest web service. And you know this method we have been using this method in all the applications here I have given home What is this this is nothing but if you fake name, so it is home dot JSP.

Because this application is going to connect to a rest web service, you need to identify the domain classes in that particular service and created over here in the client. In our case, it is a book application of a domain or model classes. So I have created the book In this class, you don't have to add the annotations that are related to the database, because it is just applied which is going to talk to the rest web service. Next, let me come to the application dot properties file, the application dot properties file, I will configure just the view resolver. We don't mean any database later details. So, this is that next let me come back to the JSP pages.

In case of JSP, if you want you can just have all the JSP pages that we have used earlier in a book application. But to make this application simple, I have created only one JSP home dot JSP from this particular JSP page itself, I am trying to invoke all the methods you can have a look I am trying to call add book method, get our books, get book by category, get one book and delete book. Of course you can try with update book also you need to have a format update book. That's it. And I am trying to retrieve a list of books using dollar books. Now, let me go into the controller in the controller, I have to retrieve the details whatever is there in home dot JSP from within the method of the controller I will be calling the method rest web service.

Let us start now, first we will try with get our books let us show all books okay. So, in the client controller, I will add a method slash get book The method is ready from within this method, we need to create an object of this template and use some method of rest template to connect to the RESTful web service. Let me start adding the code. So, I will create an object of rest template. Using this template object I am going to call get four object method. This get four objects has got its own overloaded methods, what actually is taking it is taking the URL and the response type, what is the URL, if you want to call a RESTful web service, you need to identify the URL of the RESTful web service together with a path that is I am created a registered service I am exposing all the methods in the service means I have to give it together with the URL.

So private get the URL, we have to open the browser and checking for the URL. Let me do that. So here I'm just leaving it up and go to the browser in the browser. I have got localhost 8080 spring rest one fan just copying this. And I will have this as a URL string. You are an equal to within double quotes I need to add this Okay, so now get object is taking two parameters, URL What is what actually are trying to retrieve from the bucket list of books?

So, what is the data type? What is the response type list so I can directly give this class the return type of list book booklet, sorry list book. Okay, this is that now I want to send the booklets to home that JC what we can do, we can add it to the model. That's why I'm just having Model S parameter model dot add attribute. We are given the dummy name of books that scientists party and your book list ended with a semicolon. Now, what is the path to retrieve the list of books to understand this, let us also open book controller.

It is from spring repertoire. I'm opening this book controller here it is flash books. So I have to come to this client controller I have to give it over here it says slash books. First let us try this out and check whether they are getting the output. before running the application. I want to talk about the route mapping.

So you're given the accurate request mapping slash book app. This is going to be their route mapping. If you want to get home dot JSP you cannot get it in the context path directly using something like HTTP localhost colon 8080 slash your application name slash the route mapping I have given us boot cap slash only if I do that is get home dot JSP. Let us check this out. I'm saving this application. Let me run it.

As I told you, when I'm running the client, my RESTful web service also should be deployed and running in the server. Only then you can access this application. The application has started it assigned to show the context root spring wrestling. It will be not open because our route tapis I mean, our home dot JSP can be retrieved only if you do boot camp slash. So let me click OK for this, I will just copy the URL. Now I need to go to the browser and I'm open from there.

You are seeing this white label error page let me add slash boot camp slash Yes, slash boot camp flash. Now I'm getting home dot JSP. This time I'm just adding all the method calls and home dot JSP itself are getting all books get book by category get one book delete book, what we have seen now was getting all books. We are added method to retrieve all the books from our RESTful web service. Let me click this link. Automatically it is giving me the list of books.

Basically, this restful client is trying to connect to the registered service using the URL that is being provided by the restaurants movies together with the past once the left has reached this application, actually the left is coming to this place here in which place here you're adding it to the model and sending it to home dot JSP in on that JSP you are retrieving it using dollar books just a common MVC application if you can just make the flow because of time constraint I'm trying to add all the methods I mean show all the different types in the same home dot JSP page only. Now, we are saying get books by me get our books Next let us going first get booked by category. So I will just copy this method. First what is the request mapping enorm that they see what they have given? It is like what cat does research so you have to take it over here and it is such okay this is the first part next in what actually you are using you are using your form you need to retrieve the category using records for training Client controller, I need to use a direct request Param within which I need to pass category string category now what about the URL the URL is same till the context path we will go and check whether we are using the request Param our PATH variable I'll open the book controller the book controllers from this spring wristwatch this will be service here we have completed this one the book service dot get our booth.

Now we are trying with books slash slash categories What is the meaning of it it is expecting the category as a PATH variable. Now I will just copy this asset and I will modify it you can see the URL HTTP localhost 80 spring wristwatch slash books slash search slash plus category I will close this and category I hope you can understand it is not going through the get method or POST method Just going as a PATH variable together with a URL escaping upon that. So, what actually happened that category okay. So, this is the URL again we are using get for objects first parameter see what is the second parameter is the list of books, I mean second parameter is the response type. So, it is of type list we are retrieving it and attaching it to a model Okay. Let me save this now, I will rerun the application, let me just check whether it has relaunch.

So, see output clearly in the console what you can do is you can add logger add logger boating decline program and also in all the layers of this will show the status in the controller in the service layer. And of course in the do layer we cannot give because it is taken care by spring. Okay. The application has started I'll go to the browser. I'll refresh now, I need to Going forget book by category, I will just add section click search by category. And what the output Let me explain this.

For this application you are trying to retrieve the category using query strings. Why because the value is taken from a fall and send to this particular controller. In the controller, you are retrieving it using request Param. But the other RESTful web service is taking it as a PATH variable. So, you are appending the category as a PATH variable and sending it to the back end. For this application, it is a query query string.

Can you see the stats what we are seeing? We have a data we have got, we don't know about the details of the rest web service for a user. He thinks that he is getting the list of books for the current application. But in the backend it is connecting to a full web service. Right. So this is that next letter cipher get one book and delete book.

I'll go back to it. In the client controller, let me add the method for getting one book. So I'll just copy it Ctrl C Ctrl V and what is the name from action equal to get one. So, what mapping is using it is using get mapping get one book records around this book ID book ID tuition and having a model. Now, let us form the URL till the context route ID is the same we will check for the past value what it does it is actually slash book slash one. What are the staking staking is looking in for a request for it is expecting the query I mean it is expecting the book ID to come in the query string in the request.

So, we have to I mean, we have to create the add the query string to the URL Call the restaurant service. I'll go to the client controller in the client controller, I will just modify the spot. Then question mark, book ID equal to. This is how you found the query string right? Plus category instead of category, this book ID. If you are having more than one module in that scenario, you can add it to a map and pass a map as parameter.

In our case, it has only one one book ID scientists appending it to the URL itself directly. And the template for get a template dot get four objects you are what is the return type, it is going to return only one book. So it is book dot class. So I can do saboteurs book book. I'm not going to change the name of the attribute I'll have it as books only. And here I will give book.

Okay, so this is for retrieving one book. What about get one book. I mean, the deleting a book in case of deleting a book also you are how you are trying to retrieve the value from the record for sale go to the client controller and I can use the same one. And this for this URL, I need to check in home dot JSP what it is it is delete book and the method name also allows us delete book from the records I'm trying to retrieve the book ID. Now we will form the URL and go to this book controller in the book controller it is slash books slash delete. Come to the client controller.

It is slash books slash delete. Okay, question mark book ID equal to because that metadata is trying to get the book Id only. What it is returning was this book controller delete method, it is returning a string. So in the client class, instead of book dot class you need to pass that class the state data type of a response and here and have a string result result. So now we have added two methods. One method is to get one book.

Another method is to delete one book, both a method for using request Param in the RESTful web service. So we are attaching the book ID to the URL. And we are trying to call the RESTful web service method. Let me run the application. The application has started I'll open it in the browser. The application has started I am in home dot JSP.

Now I need to check get one book and delete book further. I need the book ID. Either I can get the book ID from the database or I can retrieve it from the book list. Scientists going to click this thing show all books. I've got the list of books now we will try for this 77 book ID 77 Click Get one book Now, can you see this it is just giving this book ID 77 Let me check whether it is the same Yeah, printer over 988 Okay. So, we have retrieved one book.

Now, let us try deleting the same book 77 click Delete book book was deleted I have got the message book deleted. Now, let us check it out in the database also select star from books the book ID with 77 is remote Can you see this here it is available here it is available in this case it is not available and also we can take it from here sure books 77 is not available and what will happen if I do get book by ID 77 This book is not available. Now what it is it is an empty list. It is not even showing an error. Why? Because you're when you're trying to retrieve the book in decline controller Whatever body row got this null Okay in this place book equal to null you're trying to attach null to books attributes and when you are trying to retrieve using dollar books it'll say expression language because the value is not available it is just showing an empty page empty result you can just do i mean checking you book is null then model dot add attribute books come on no book available you can just give appropriate error messages okay this is for getting one book and deleting book.

Now let us see how to add book to the database from a restful client. For this we are going to use force for object method that we first add the method here it is in home dot j fetus add book in client controller, I need to go in for add book method. It is also add book Okay. Yes and is going to have boop boop responsible for creating the book object and populating the value spring. Now let us find the URL, I will go to this book controller and book where it is here it is slash books only, but the method is post, okay? No books only till the spark.

Now I'm using this template instead of get for object I need to go in full force for object. So let me add the method we'll see what actually we have to do over here. Control space pose for object, you can see that there are three overloaded methods for posts for objects. The store for object takes three parameters, the URL, the request and the response type. What is the URL you know what is the request when you are using post or when you're trying to send the data using POST method in that scenario, all the values are carried only in the body part. Here also Just coming only in the body part because here also we have used force only records mapping add up of use force only, I think it is called home dot JSP.

Let me just check it out. Yeah, here it is post. So, I need to go in first for snapping. So, here what I have to do, I have to retrieve all the values from this book object attaches to a parameter, I mean attach it to a map and send it to be refunded service, so that the data is carried in the body part of the request. And whatever the third parameter, the third parameter is the response state for boot controller, what is the response that we are giving add books? Look at it.

So it is of type string. So let me just do that. For us for objects. The first parameter is the URL. The second parameter is the Param. Data Param map.

And the third parameter is string dot class. It does suggest show me book Asara to the database What does this current map we are going to pass multiple values. So I need to go in for my value map. Let me add that. In an object of map, you can see this map my c value map, we have learned about maps from Java dot util. This is from spring framework, whenever you want to send a chunk of data, you will which is using force method means you can go in for this multi value map.

Now I can just use fire a map.as. The first parameter is the I mean, the property name which is available in the other book club. So it's a title here I need to retrieve it right. So what I have to do book dot get title, I hope you can understand this. This book dot get title is coming from the parameter over here, and I'm appending it to title so that when it is going to this book controller, it can be retrieved by This book object because it can be assigned to this book object why that there is also a book class which is having the same instance variable as that of we have gotten this claim both should match if you are instead of giving title if you are giving name then in that scenario in the book controller it will go in search of named instance variable in the boot class, okay.

So, we have to match the properties. So, similar to this I need to do for all the other properties title, book ID, price category and author let me do that and of course, let me explain this, I am trying to retrieve individual properties from this book object. This book object is big basically populated by spring framework from the form fields. I am stripping out one by one value and assigning it to a few names. The names that are given over here should match the properties of the model class or domain class in a RESTful web service interaction To like not go in for name, instead of to come in what is that book ID I cannot go in for ID I have to match the properties which are there in the model class properties of the model class. Okay, this is done, then I need to go in for I need to have what Model Model, what it is returning it is going to simply return Bukhara to the database.

So, the first parameter for ports for objects is the URL. The second parameter is a pair of the request details that you want to send to the RESTful web service. The third parameter is the return type that the service is returning to the client. Okay, now I'm getting the return type and I'm assigning it to books it is irrelevant I am just showing you how it can be retrieved you try to give appropriate messages like say model dot add attribute result commercials or message commerce or something like that. Now let me save this. I will read and application you don't have to restart the application every time you Can renounce application we can you see this here you've got three non red button with the green triangles if you do that your application will run still more faster.

The application has started I'll open it in the browser. I've got home dot JSP. Let me try adding book. I have added data let me click Add book could immediately as book added, you can see in the URL we are seeing the URL of the spring MVC application only. What happens in the back end we really don't know it is actually connecting to another service and storing the data in the database. Let us check that out and just open in the database here.

Select star from books. Yeah, just now we are at couchdb two sided over here. We can also take it from the application by clicking show all books. We can check whether it is added yeah decided over here. consuming a RESTful web service using a spring MVC application. We have seen how to consume all the methods I have not shown only updates You can try it out.

Try that out. There are different ways of consuming a RESTful web service. Like using the different methods. Instead of going in for force for object you can use force for entity instead of get for object you can use get for an entity and so on. I have shown you the easiest ways so that you can understand how a restful client can how our client can access the RESTful web service.

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.