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
€67.10
List Price:  €95.86
You save:  €28.76
£55.67
List Price:  £79.54
You save:  £23.86
CA$100.49
List Price:  CA$143.56
You save:  CA$43.07
A$111.96
List Price:  A$159.95
You save:  A$47.99
S$94.87
List Price:  S$135.54
You save:  S$40.66
HK$544.16
List Price:  HK$777.41
You save:  HK$233.24
CHF 62.56
List Price:  CHF 89.37
You save:  CHF 26.81
NOK kr792.29
List Price:  NOK kr1,131.89
You save:  NOK kr339.60
DKK kr500.54
List Price:  DKK kr715.08
You save:  DKK kr214.54
NZ$123.74
List Price:  NZ$176.78
You save:  NZ$53.04
د.إ257.07
List Price:  د.إ367.26
You save:  د.إ110.19
৳8,330.24
List Price:  ৳11,900.85
You save:  ৳3,570.61
₹5,945.56
List Price:  ₹8,494.03
You save:  ₹2,548.46
RM315.51
List Price:  RM450.75
You save:  RM135.24
₦108,149.19
List Price:  ₦154,505.46
You save:  ₦46,356.27
₨19,403.53
List Price:  ₨27,720.51
You save:  ₨8,316.98
฿2,393.75
List Price:  ฿3,419.79
You save:  ฿1,026.04
₺2,454.67
List Price:  ₺3,506.82
You save:  ₺1,052.15
B$425.95
List Price:  B$608.53
You save:  B$182.58
R1,282.09
List Price:  R1,831.63
You save:  R549.54
Лв131.15
List Price:  Лв187.37
You save:  Лв56.21
₩101,234.93
List Price:  ₩144,627.53
You save:  ₩43,392.60
₪254.67
List Price:  ₪363.83
You save:  ₪109.16
₱4,117.86
List Price:  ₱5,882.91
You save:  ₱1,765.05
¥10,949.58
List Price:  ¥15,642.93
You save:  ¥4,693.35
MX$1,405.49
List Price:  MX$2,007.92
You save:  MX$602.43
QR254.12
List Price:  QR363.05
You save:  QR108.92
P963.49
List Price:  P1,376.48
You save:  P412.98
KSh8,999.72
List Price:  KSh12,857.29
You save:  KSh3,857.57
E£3,561.31
List Price:  E£5,087.81
You save:  E£1,526.49
ብር8,689.79
List Price:  ብር12,414.52
You save:  ብር3,724.72
Kz64,250.82
List Price:  Kz91,790.82
You save:  Kz27,540
CLP$69,143.42
List Price:  CLP$98,780.55
You save:  CLP$29,637.13
CN¥510.67
List Price:  CN¥729.56
You save:  CN¥218.89
RD$4,244.94
List Price:  RD$6,064.47
You save:  RD$1,819.52
DA9,440.04
List Price:  DA13,486.35
You save:  DA4,046.31
FJ$162.13
List Price:  FJ$231.62
You save:  FJ$69.49
Q537.12
List Price:  Q767.35
You save:  Q230.22
GY$14,584.29
List Price:  GY$20,835.60
You save:  GY$6,251.30
ISK kr9,693.35
List Price:  ISK kr13,848.23
You save:  ISK kr4,154.88
DH701.59
List Price:  DH1,002.31
You save:  DH300.72
L1,285.64
List Price:  L1,836.70
You save:  L551.06
ден4,127.89
List Price:  ден5,897.23
You save:  ден1,769.34
MOP$558.06
List Price:  MOP$797.27
You save:  MOP$239.20
N$1,283.39
List Price:  N$1,833.49
You save:  N$550.10
C$2,565.21
List Price:  C$3,664.75
You save:  C$1,099.53
रु9,482.30
List Price:  रु13,546.73
You save:  रु4,064.42
S/259.58
List Price:  S/370.84
You save:  S/111.26
K282.68
List Price:  K403.85
You save:  K121.16
SAR262.90
List Price:  SAR375.59
You save:  SAR112.68
ZK1,929.21
List Price:  ZK2,756.13
You save:  ZK826.92
L333.95
List Price:  L477.10
You save:  L143.14
Kč1,686.22
List Price:  Kč2,408.98
You save:  Kč722.76
Ft27,781.83
List Price:  Ft39,690.03
You save:  Ft11,908.20
SEK kr772.17
List Price:  SEK kr1,103.14
You save:  SEK kr330.97
ARS$71,242.69
List Price:  ARS$101,779.64
You save:  ARS$30,536.94
Bs481.71
List Price:  Bs688.19
You save:  Bs206.47
COP$305,135.87
List Price:  COP$435,927.07
You save:  COP$130,791.20
₡35,171.10
List Price:  ₡50,246.58
You save:  ₡15,075.48
L1,769.55
List Price:  L2,528.04
You save:  L758.48
₲543,563.42
List Price:  ₲776,552.46
You save:  ₲232,989.03
$U3,109.25
List Price:  $U4,441.97
You save:  $U1,332.72
zł286.15
List Price:  zł408.81
You save:  zł122.65
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.