15. Converting Spring MVC application to Restful Service

18 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
€65.09
List Price:  €93
You save:  €27.90
£56.02
List Price:  £80.04
You save:  £24.01
CA$96.21
List Price:  CA$137.46
You save:  CA$41.24
A$106.50
List Price:  A$152.16
You save:  A$45.65
S$94.88
List Price:  S$135.55
You save:  S$40.67
HK$547.08
List Price:  HK$781.57
You save:  HK$234.49
CHF 63.59
List Price:  CHF 90.85
You save:  CHF 27.25
NOK kr764.52
List Price:  NOK kr1,092.22
You save:  NOK kr327.70
DKK kr485.54
List Price:  DKK kr693.66
You save:  DKK kr208.12
NZ$116.87
List Price:  NZ$166.97
You save:  NZ$50.09
د.إ257.07
List Price:  د.إ367.25
You save:  د.إ110.18
৳7,696.46
List Price:  ৳10,995.41
You save:  ৳3,298.95
₹5,845.02
List Price:  ₹8,350.38
You save:  ₹2,505.36
RM331.92
List Price:  RM474.20
You save:  RM142.27
₦98,297.45
List Price:  ₦140,430.95
You save:  ₦42,133.50
₨19,467.44
List Price:  ₨27,811.82
You save:  ₨8,344.38
฿2,586.11
List Price:  ฿3,694.61
You save:  ฿1,108.49
₺2,257.70
List Price:  ₺3,225.42
You save:  ₺967.72
B$355.19
List Price:  B$507.43
You save:  B$152.24
R1,300.44
List Price:  R1,857.85
You save:  R557.41
Лв127.53
List Price:  Лв182.19
You save:  Лв54.66
₩95,343.18
List Price:  ₩136,210.38
You save:  ₩40,867.20
₪259.39
List Price:  ₪370.57
You save:  ₪111.18
₱4,014.38
List Price:  ₱5,735.07
You save:  ₱1,720.69
¥10,876.02
List Price:  ¥15,537.84
You save:  ¥4,661.82
MX$1,183.66
List Price:  MX$1,691.01
You save:  MX$507.35
QR254.83
List Price:  QR364.06
You save:  QR109.23
P952.42
List Price:  P1,360.66
You save:  P408.23
KSh9,168.69
List Price:  KSh13,098.69
You save:  KSh3,930
E£3,329.50
List Price:  E£4,756.64
You save:  E£1,427.13
ብር3,992.74
List Price:  ብር5,704.16
You save:  ብር1,711.42
Kz58,583.02
List Price:  Kz83,693.62
You save:  Kz25,110.60
CLP$65,554.03
List Price:  CLP$93,652.63
You save:  CLP$28,098.60
CN¥505.70
List Price:  CN¥722.46
You save:  CN¥216.76
RD$4,071.34
List Price:  RD$5,816.45
You save:  RD$1,745.10
DA9,415.49
List Price:  DA13,451.28
You save:  DA4,035.78
FJ$159.21
List Price:  FJ$227.46
You save:  FJ$68.24
Q544.84
List Price:  Q778.38
You save:  Q233.53
GY$14,671.19
List Price:  GY$20,959.74
You save:  GY$6,288.55
ISK kr9,784.60
List Price:  ISK kr13,978.60
You save:  ISK kr4,194
DH703.12
List Price:  DH1,004.51
You save:  DH301.38
L1,240.96
List Price:  L1,772.88
You save:  L531.91
ден4,012.27
List Price:  ден5,732.06
You save:  ден1,719.79
MOP$564.93
List Price:  MOP$807.08
You save:  MOP$242.14
N$1,294.05
List Price:  N$1,848.73
You save:  N$554.67
C$2,581.34
List Price:  C$3,687.79
You save:  C$1,106.44
रु9,369.68
List Price:  रु13,385.83
You save:  रु4,016.15
S/261.57
List Price:  S/373.68
You save:  S/112.11
K271.48
List Price:  K387.85
You save:  K116.36
SAR262.48
List Price:  SAR374.99
You save:  SAR112.51
ZK1,900.47
List Price:  ZK2,715.07
You save:  ZK814.60
L323.94
List Price:  L462.80
You save:  L138.85
Kč1,630.83
List Price:  Kč2,329.86
You save:  Kč699.02
Ft25,346.53
List Price:  Ft36,210.88
You save:  Ft10,864.35
SEK kr763.26
List Price:  SEK kr1,090.42
You save:  SEK kr327.16
ARS$61,644.55
List Price:  ARS$88,067.42
You save:  ARS$26,422.86
Bs483.68
List Price:  Bs691.01
You save:  Bs207.32
COP$273,058.61
List Price:  COP$390,100.45
You save:  COP$117,041.84
₡35,861.91
List Price:  ₡51,233.50
You save:  ₡15,371.58
L1,732.92
List Price:  L2,475.71
You save:  L742.78
₲523,307.77
List Price:  ₲747,614.58
You save:  ₲224,306.80
$U2,685.15
List Price:  $U3,836.09
You save:  $U1,150.94
zł280.89
List Price:  zł401.29
You save:  zł120.40
Already have an account? Log In

Transcript

In this session, let us learn how to convert a spring MVC application into a RESTful web service. Already we have learned how to create a RESTful web service using Spring Boot. In that case, the packaging was just here, we are going to have the packaging as what we are going to modify an existing application into a spring RESTful web service. Let me just show you the steps. Here you need to create a Spring Boot application as well as the starter dependency the packaging should be one then you need to create a class a simple Java class the class should be annotated with accurate risk controller. Then as usual, you will be having an application class which extends Spring Boot servlet initializer.

Then, in the application that properties earlier we will configure configuring internal resolver now internal resource view resolver. Now, we don't have to do this, because this is not a web application, but this is a RESTful web service, which is exporting the method using a rest controller so that an attacker getting exposed these are the two methods which are getting exposed, get booked by ID and get all books that access mapping is book details fulness book details slash book ID. Here I have given it within curly braces, it means we are using PATH variable, you know what is the use of request Param when you're having your farm, if you want to retrieve the values from the farm, which is using the method of get in that scenario, you need to use a cos Param. If you are having fear method is you don't have to go into a request para automatically it will be populated to the domain or the modern objects if the value is passed in the URL itself.

That scenario to retrieve the value you have to go in for at the rate PATH variable. So, what actually we are going to do now is we will go back to eclipse we will try to convert the existing spring MVC web application into a RESTful web service. I am an eclipse I have created a Spring Boot application using spring starter project and I have added spring data JPA dependencies and also I have got my SQL connector Java we are going to work on the same application that we have done earlier wherein we tried to connect a spring MVC application to the database using spring data JPA I have copy pasted all the classes from that application. Let me first go through the flow in combat boot camp I have the application class which extends system sorry which extend Spring Boot servlet initializer. Then I have bought the book controller then Got the model classes book, book service book service I am PL and book repository.

No change. I am copy paste in all the classes, what about the JSP pages, we are going to expose the methods as a RESTful web service. So we don't need any JSP pages. Actually, these methods will be called by a client. The client can be any type of client client, it can be an Angular application or a normal Java application or a simple spring application or even it can be a spring MVC application. So, we will be learning how to consume the methods of TLS web service in the next session.

Now, we will try to convert all the methods that we have got into a RESTful web service. That is this is a RESTful web service, which is exposing few methods to perform crud operation with the database. This is where the Self Service As a client, client deciding I mean client wants to retrieve the list of books, say saying to access the request service client wants to restore the tree. The list of books by category, again connects to the restaurant service client wants to add a book to this particular database use arrestor service that is what we are going to do no change in the backend part that is in the service and the repository. What about application dot properties? In application dot properties file?

Actually, you have given configuration details for internal resource view resolver. We don't need this now, right because you are going to expose the methods of RESTful web service, these methods are being called only by the client. This is not a standalone web application. You need details that are needed to connect to the database. So I have just maintained that now let me go into this book controller. In the book controller, we are annotating this method with the rate controller what me if you want this to be a MVC application, you will need to annotate this at the rate controller.

Now, this is a RESTful web service. So what do you need? You must go in for the rate rest controller. What about the dependencies? Do we need to add any dependencies is not required when you give the application starter packages Well, in that case, automatically the jar files later to web then, yeah, JAR files related to MVC, then hibernate validator, then Jackson jar there will be downloaded and added to your application. So you don't have to add any other extra JAR files.

I don't need the session attribute. Then. Now let me just go step by step. First thing is I want to get all the books okay. So here I will just change the method name I will just comment everything we will see one by one. Here our direct request mapping slash To retrieve all the books, okay, and here I don't need model dot add attribute let me just have the method name is get all books.

Now I have got the list, I am just going to return the book list. Remember this is not a web application in case of a web application whatever you are returning will be the name of the view page here it is just a list. So, in the browser you will be getting only the list of books in JSON format string here it has to be liskov book so I will copy this. So this is for getting all books. Now I will I have got other methods also. First I want to show you how this application is by running this application.

How to open it in the browser. In the browser. You need to open it as localhost colon 8080. Slash application name that is your context. Ruth slash your PATH variable in our case to us books, so it is http colon slash slash localhost colon 8080 slash spring rest what this is my application name which is nothing but the context root slash books. Okay, now, I will go back here.

Let me run this application, select spring rest one, right click run as run on server, remote other applications which are there. Click Finish, the application has started because this is a RESTful web service. And the result is going to be in the form of JSON. You cannot see it from the eclipse browser you need to open it in the in an external browser only. Let me click OK. I need the URL file.

Just keep the cursor here. Now I have got spring wristwatch. I will copy this Ctrl C. I'll go back and I'll open the browser. I have added the UI When I press enter, I am getting white label error page. Why? Because for this URL, we don't have a specific JSP page.

That is why we are getting this white label error page, if you want to consider it and if you want to get a user friendly error message, of course, you can just create your a dot JSP in the in inside the buy enough folder. Now, we have bought the path of books. So let me add slash books. You can see the output I have got the list of books. So my RESTful web service is exporting get our method using the slash books PATH variable, whichever client wants to get the list of books, he can just call the URL to retrieve them get a list of books. Similar to that let us try with other methods also.

I'll go back a clip. Here I've got two windows let me click OK. Now I this is my book controller. Okay, what about the second method Do we need the second Method No, it is just to show the form. I don't need this next port mapping slash add book. Well, what is the meaning of course mapping it means that you are having a form from the form you are trying to send the values using force method. Yeah, if you want to actually test this it is not possible either you're too going for a testing tool like I mean, you can use a tool like swagger our postman to check this method on what you can do you can create a consumer which will try to consume this method.

So, this first mapping alone we cannot see and just are okay what about this redirect? In case in case of this return, what I need to do I also send a message I have to return a message saying success are successfully added our error. Now let me just add have a error success message Are you can send the response entities water star outcome of this method you can just return now simply just returning success, then I don't need this get update form. Next method I am trying to get booked by ID passing that by passing the ID. So what we will do we will just change the method name get booked by ID with a slash books slash one okay and I'm using request para book ID it means what actually you What is the meaning of it this value is sending a request from a form which is having the method of get in case of get water you can do you can just go in for the very thing right.

So I will try to make the query string and return one book object. So I will show you that here I will just give it a slash and just commenting this stuff the part in this I don't need model attribute and remove this model parameter also Get by ID What do you want to return you want to return book and the return type here this book Okay. Now, let me save this I will rerun this application you can have started let me go to the browser and open it. So, here it is slash book slash one. After that question mark book ID, I hope you can understand this. If you see here and just close this and open Eclipse in book controller we are given slash book slash one.

We are using request Param request Param What is the name book ID so far book ID you are supposed to give the value you would have I you don't have any restful consumer. What you have to do, you have to explicitly make the query string. If you find it difficult, you can just use that tool like swagger, our postman to check this method. Now I will I'm going to do it directly I'll open the browser here book ID after the query string one question mark book ID equal to we already had one book with the ID 14 Let me press Enter getting any error message because it has to be books as given a test book. Let me press enter Now I brought the output Okay, this was the book which we are added in the previous session. Okay, next let me come back here, we have seen how to retrieve all the books retrieve one group.

In case of course mapping I have given it a slash ad group. This is not the right naming convention, you need to go in for the entity name which I'm in modern law domain name with plural notation. So I need to go in for books only. When it's not the admin was ambiguous URI or using slash books again, here also you're using slash books. But here the method is get here the method is post. So, it is not ambiguous, but what about this get mapping again if you are using books alone in that case it will be ambiguous.

So, I have given slash one. Now this is done next I want to update book for updating bukata I told you I need to view all the values from consumer so I'm just leaving this method off next I will come for delete method slash stuff for delete this is not needed. Okay, for delete book what we will do is we can just go in for the same what books slash delete slash here either we can go in for path by PATH variable or we can use request Param. So, here already there is a request para I will just leave it assets to go with this letter. See how to Use this get by category forget obey category I don't want to use sec cascara I want to use PATH variable how to do PATH variable let me just show you. So, here this books slash search slash within curly brace category.

So, when you are trying to call this method you are supposed to give the category after the slash, no need of any question mark name value directly you can give the category okay. And now, instead of request Param what it is, it is PATH variable path Param we have used when we were working with RESTful web services. I don't need the model port also. And I don't need any of these return booklist the Book is not Available what he can do, he can throw an exception and instead of throwing an exception directly to the user, you can just give an error message also that is fine. But now I'm just going to show them the star books that's it and for delete book what here book deleted okay Add To Cart I am changing the method to book added slash after this delete okay this is that now let me run the application again.

Now application has started let me open the application in the browser side with delete delete question mark book ID equal to 40 I'm trying to delete the book that we have seen now that we press Enter book is deleted and I also want to check the book by category so it is slash books slash search slash category. Okay, we are it is not request Param you are not passing using get method but your test pass variable. So directly I can do the category name feature that we press Enter. Now I am getting the list of books based on a particular category we will try with tech also although they have a stick okay. And we will also check whether 14 is deleted slash books and you cannot see from here what I will do I'll just open it from the database the last book of spring data JPA Let me press enter that book is deleted.

Can you see this? Here doneness, we have converted a spring MVC application which is performing crud operation with the database into a RESTful web service. We have tried performing all the operations, I mean not all the operations of course, retrieve then delete, then again get one book then get booked by category that also we have checked. for adding a book and for updating a book. We cannot check it over. If you want to check for adding Of course, you have to give all the values like for me for book ID title for everything you need to give the values, but here the method is post.

So, you cannot give the values in the query string, if you want to check using force method, either use a tool or use a consumer. Now, I will go back to this boot controller in the controller we are having for snappy, I will remove this ID I don't need this model attribute also, I have changed the method parameters and also I have changed the mapping. So, this is the restful endpoint which is exporting the methods, the methods to retrieve all the books books by category returning one book and adding updating deleting books to the database. Now, what we need to do is we need to create a consumer which will try to consume all the methods of this restful service. So, in the next session, let us create a simple spring MVC application Which will consume all the methods of this particular RESTful web service consuming swamp basically caught trying to call the methods.

These two applications are totally different. One application is trying to call the method some other application. Okay. We will see how to create a REST client in the next session. Thank you

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.