15. Spring boot as Jar

17 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
€66.35
List Price:  €94.79
You save:  €28.44
£55.27
List Price:  £78.97
You save:  £23.69
CA$97.91
List Price:  CA$139.89
You save:  CA$41.97
A$107.61
List Price:  A$153.73
You save:  A$46.12
S$94.02
List Price:  S$134.32
You save:  S$40.30
HK$544.76
List Price:  HK$778.26
You save:  HK$233.50
CHF 61.99
List Price:  CHF 88.57
You save:  CHF 26.57
NOK kr773.52
List Price:  NOK kr1,105.08
You save:  NOK kr331.55
DKK kr494.87
List Price:  DKK kr706.99
You save:  DKK kr212.12
NZ$119.04
List Price:  NZ$170.06
You save:  NZ$51.02
د.إ257.07
List Price:  د.إ367.26
You save:  د.إ110.18
৳8,334.01
List Price:  ৳11,906.24
You save:  ৳3,572.23
₹5,905.12
List Price:  ₹8,436.24
You save:  ₹2,531.12
RM312.96
List Price:  RM447.10
You save:  RM134.14
₦117,219.95
List Price:  ₦167,464.25
You save:  ₦50,244.30
₨19,380.58
List Price:  ₨27,687.73
You save:  ₨8,307.15
฿2,429.14
List Price:  ฿3,470.35
You save:  ฿1,041.21
₺2,412.08
List Price:  ₺3,445.98
You save:  ₺1,033.89
B$405.06
List Price:  B$578.68
You save:  B$173.62
R1,267.31
List Price:  R1,810.53
You save:  R543.21
Лв129.73
List Price:  Лв185.34
You save:  Лв55.60
₩97,779.80
List Price:  ₩139,691.42
You save:  ₩41,911.61
₪261.55
List Price:  ₪373.67
You save:  ₪112.11
₱4,130.77
List Price:  ₱5,901.36
You save:  ₱1,770.58
¥10,901.93
List Price:  ¥15,574.86
You save:  ¥4,672.92
MX$1,414.26
List Price:  MX$2,020.46
You save:  MX$606.19
QR254.33
List Price:  QR363.35
You save:  QR109.01
P948.83
List Price:  P1,355.53
You save:  P406.70
KSh9,063.70
List Price:  KSh12,948.70
You save:  KSh3,885
E£3,478.50
List Price:  E£4,969.50
You save:  E£1,491
ብር8,583.46
List Price:  ብር12,262.61
You save:  ብር3,679.15
Kz63,877.12
List Price:  Kz91,256.94
You save:  Kz27,379.82
CLP$67,980.58
List Price:  CLP$97,119.28
You save:  CLP$29,138.70
CN¥507.18
List Price:  CN¥724.58
You save:  CN¥217.39
RD$4,200.06
List Price:  RD$6,000.34
You save:  RD$1,800.28
DA9,338.81
List Price:  DA13,341.73
You save:  DA4,002.91
FJ$158.92
List Price:  FJ$227.04
You save:  FJ$68.12
Q538.38
List Price:  Q769.16
You save:  Q230.77
GY$14,583.53
List Price:  GY$20,834.51
You save:  GY$6,250.97
ISK kr9,640.42
List Price:  ISK kr13,772.62
You save:  ISK kr4,132.20
DH696.78
List Price:  DH995.44
You save:  DH298.66
L1,273.82
List Price:  L1,819.82
You save:  L546
ден4,077.60
List Price:  ден5,825.40
You save:  ден1,747.79
MOP$559.09
List Price:  MOP$798.74
You save:  MOP$239.64
N$1,260.89
List Price:  N$1,801.35
You save:  N$540.46
C$2,566.52
List Price:  C$3,666.61
You save:  C$1,100.09
रु9,423.16
List Price:  रु13,462.23
You save:  रु4,039.07
S/264.65
List Price:  S/378.10
You save:  S/113.44
K280.62
List Price:  K400.90
You save:  K120.28
SAR262.75
List Price:  SAR375.38
You save:  SAR112.62
ZK1,926.57
List Price:  ZK2,752.36
You save:  ZK825.79
L330.17
List Price:  L471.69
You save:  L141.52
Kč1,676.98
List Price:  Kč2,395.79
You save:  Kč718.80
Ft27,187.34
List Price:  Ft38,840.72
You save:  Ft11,653.38
SEK kr769.78
List Price:  SEK kr1,099.74
You save:  SEK kr329.95
ARS$70,112.88
List Price:  ARS$100,165.56
You save:  ARS$30,052.67
Bs483.65
List Price:  Bs690.96
You save:  Bs207.30
COP$308,470.35
List Price:  COP$440,690.82
You save:  COP$132,220.46
₡35,450.59
List Price:  ₡50,645.88
You save:  ₡15,195.28
L1,762.11
List Price:  L2,517.41
You save:  L755.29
₲543,153.02
List Price:  ₲775,966.15
You save:  ₲232,813.12
$U2,992.07
List Price:  $U4,274.58
You save:  $U1,282.50
zł287.83
List Price:  zł411.21
You save:  zł123.37
Already have an account? Log In

Transcript

In this session, let us learn how to create a RESTful web service using Spring Boot. For this, you need to add web dependency, the application started packages Spring Boot starter web. What are the jobs that are inside this the package Spring Boot spring jar Jackson just because your work you're going to convert your application into restful service, hibernate validated and embedded Tomcat, the Tomcat server will be embedded with your application Now, now, let us see what are the steps that are needed to create a rest web service create a Spring Boot application select the packaging as jar then create a controller create a main class run the application. Let us see one by one. What is the controller? Yes class that is annotated with at the rate risk controller is called a C controller this This only exposes application as a RESTful web service.

Here you can have many methods with the routing details can return one method great message, which returns a string, this method is annotated with at the rate request mapping, this request mapping is used to provide the routing details, I have given as a slash home. From within this method, you can call the other layers service service talking to do like that, in the previous application from public static void Main, we were trying to call the book service the book service was calling to this calling the book TiVo. Now, when we are exposing our application as this web service from within the methods of controller, you will be calling the other layers that the C service layer. So from within this particular method, you can call the methods of book surveys. It goes on like that. Now, how to access this application When you want if you want to access this application as a RESTful web service, you want to go to the browser and do localhost colon 8080 8080 is the port number for embedded Tomcat slash the routing details, hope when I give slash for in the browser, I will get the output what output whatever I am returning Welcome to Spring Boot, I can return a string array of objects or a single object, what actually it gets, I mean, will it print as an object?

No, in this case, it will be converted into JSON and it will be shown as output in the process. Because we have added Jackson jars when we add them automatically this Jackson jars will be added. So, the Java object or list of Java objects will be converted into JSON. This is for the controller. Next, let us see the main class. The main class will have public static void Main and it will be annotated with Spring Boot application.

You're calling The run method of spring application, which will bootstrap the application now, for running it, select this file run as Java application, run a Spring Boot app, create the jar file and run the jar file directly. When you run the application, Tomcat server will be started so that you can see the output in the browser. You need to do localhost colon 8080 slash the routing details. In this case I have given four so the slash home now, let us try this application in the in Eclipse. Let me go back to eclipse. I am an eclipse now let me create the spring starter project.

That means you the project name of Spring Boot is just type this Maven packaging is just left I'm just going with the defaults. The package name is comm dot example dot training. Click Next. Now I need to select the dependency the dependencies with so go here And type with select web. Click Next. Click Finish the application is getting created.

Application is ready. Let me open the application palm dot XML okay here Can you see the dependency that we have added to show Spring Boot starter web. Now we will see that dependent JAR files. I'll go to the dependency hierarchy you can just have a look Spring Boot starter web is having starter as dependency within which core and context is added. Then logging is there. Then what is Tomcat embedded Tomcat is added then hibernate validator is added then Jackson's related database are added.

Okay, so this was for the pom dot XML part. Now I'll go to this main main application SRC main Java comm dot example dot training within which I have got Spring Boot as our application This is a simple public static void Main only here we are going to convert it into a rest web service so we don't have to implement command lines unless I have a simple print and statement creating just now the system next let me create the create a class which will be annotated with at the rate risk controller. So select the package right click new class I don't want to go in the same hierarchy so I'm just going in for comm dot example dot book cap. Now within this let me give the class name is book controller. We are going to add the classes that we have done the previous program the book service and book the book First we will see a simple program and then we will modify it click Finish.

The first thing is I need to annotate this class with address rest controller. Next, from within this I need to create a method Let me have a simple Method greet message which returns a string, the method is ready I need to annotate this method with at the rate request mapping to do the routing details. So let me add that at the rate request mapping within the round brackets double quotes slash home so the controller is ready. Now I'll go back to the Spring Boot jar application that class which is having public static void Main. How to run this application I need to run it a Spring Boot app RS Java application. Once I run run this application, automatically the Tomcat server will be started in port number 8080.

The web application context will be the IOC container because we have added Weber's dependency. We have not learned about spring MVC just understand this. If it is a simple spring application we will be having our own IOC container because we are going to we are using accurate rest controller we will be having as the Application context also. Now, here, if you see the package hierarchy, these two are two different hierarchies. So what I need to do I need to go in for a conference can, I will I can change the hierarchy. So I'm just going to refactor this part this class which is having public static void Main and just going to hurt us calm dot XML.

So calm that XML package is having the class the public static void Main, which is annotated with at the rich Spring Boot application and the sub package of this bootcamp comm.example.com. You can see that that is a yes symbol meaning to say that this is a spring beam. Now let me save this, I need to run this application. So here Spring Boot has control if I told you we can run it as a Java application, our Spring Boot apps both are saying that please application has started. We've got the output of creating the Now, if you see there are multiple lines of code, why the reason is we have our web application context and also it will just show you the mapping part. The different types of mapping that we have done, we have done mapping for slash for to which method we have done, we have done it to public Java dot Lang dot string comm dot example dot boot camp dot controller, the method name is great message okay and see that Tomcat server is started in port number 8080.

Okay, now it means the application has started the server is running, you need to go to the browser to check in for the URL. Let me open the browser. So just give localhost colon 8080 slash home center. Now you need to get the output Have a good day output. That is this application is exposed as a RESTful web service any client can access to This RESTful web service using the URL the URL is localhost colon 8080 slash home okay, now I have got the output let me just go back to the program. Let me add two more layers that is I want to add the service and the DA release.

I will just go and take it from the previous program Spring Boot spring them all got the one service and just copying it and they will add it here. Okay, they are in a different hierarchy. So let me change the hierarchy also comm.example.bootcamp.da boo. I'm changing it for service also combat example dot bootcamp dot service. Click ok. I have modified the name.

Now in combat example I have got the class that is annotated with Spring Boot application. In boot camp I have got book controller in the sub package of boot camp. I've got two packages do one service do is having to do MPLS D or MPLS annotated with a direct repository. In service. I've got book book service and book imp, even book what you can do, you can just put it in a bean package, comm dot example dot book app dot B and now we're about book service and book service imp. This is annotated with a direct service and auto wired this book do now from this book controller, I need to call the methods of book service.

So for that, let me just create two methods. Okay, now what I have to do I have to call the method. So here, I need to have an instance of book service. And this should be annotated with at the rate auto wired. Okay. Now from here what I can do, I can call the method rate book service dot get book by ID.

Okay, it's not important. That's why it's giving me an error. Let me import everything, get the content SS control space, get book by ID. So here it is return return book service dot get book by ID. So now Now I need to annotate this method using our the rate request mapping. Now I need to give the URL slash book slash, I also give the value for this ID, how to get the value for ID.

We have learned about path Param and query Param in RESTful web service, right, so here we are going to use that within curly brace, we are just going to pass the value whatever the user is entering will come here as slash book slash one slash book slash two like that. Now here I'm just giving it a ID. And in this case, it does not pass parameter it is PATH variable at the rate path variables within the own brackets actuators ID Because our same to this ID to avoid confusion I can have it as book ID I hope it is clear you are passing value which will be taken over here and it is our same to this ID and from here you are assigning it to book ID okay. So, this is that let us call get our books method of book service. Let me add one more method here public list book get all books return again book service dot get all books in this case also I need to have record mapping and Avatar slash books.

Okay, this is that let me import list did we do auto wiring? Yeah it is also completed. Let me save this now. All set, let me read an application, you can just see in the console, the server is still running. So what actually you need to do, you have to stop it and restart the application or on the top, you're having your red scrap together with the green triangle. So you just click it, it is used to relaunch the application, we keep the cursor itself we will get the message we launched Spring Boot jar application.

Now the application is restarted, it may open the console. And check it out here whether the mapping has come boots, book ID books, all the three mapping says come I'll open the browser now. I will refresh install form now I need to give books. It's giving me the list of books that we have added actually it is coming from the do layers in do layer we have hard coded the values basically it will be coming from the database and also book slash ID book slash one In case of one it has to give Java in action any other value has to do Spring Boot okay you can see the output. So, what actually we are seeing is how to convert a spring application into a RESTful web service. We have got all the layers just like a spring application, we have got all the layers.

You can just see this dmo then service even book actually you can just move it to a different package move create package, instead of service it has to be being you can view our domain you can just do our model you can do it depends on the project you're working with. As of now I am just giving it this beam. Can you see it's a complete layered application wherein we have not used database instead I have just shown you the hard coded values. Let me just show you one more thing also how to run this application as a jar file from command line. So for this, I need to create a database how to create it, you have to select the project right click run a Maven build, you need to package it right. So you can just do package click apply and now the jar file is created in this particular location, the drive whatever location I have got, can you see target from within this I have got the Spring Boot has just Ctrl C. Let me just open it in this folder.

Can you see the Spring Boot has jar the jar file is created. Now let me just go into this command command prompt. From here I need to run it Java hyphen jar space, how to give the file name. click Paste the jar and you press Enter just like what you got in Eclipse you will be getting it and now you can just open the application liberal sir. Okay, why the application has failed because you already have run it over here, right? I need to stop it and then rerun it.

I hope you can understand already the Tomcat server was running in that particular application. That's why it was giving an error and the application fails to start. The application is started. You can see Tomcat started in port number 8080. Can you see this in this line? Okay, Tomcat started in port number 8080.

And it is also showing the output creating just now go back to the browser and check the output. I'm in the browser. Let me refresh. And also I can try with one can you see and books. So this is how you will create a spring RESTful web service using Spring Boot. I have shown you how to run the application as a Java application as a Spring Boot app and from the command line using the jar file.

That you have got. In the next session let us learn how to work with Spring Boot application and JDBC using Spring Boot. 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.