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
€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

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.