REST Applications with Spring

Project Setup and Starting App

Your browser needs to be JavaScript capable to view this video

Try reloading this page, or reviewing your browser settings

This video segment will teach you how to add dependencies to your Spring Boot project. You will learn to create and define a starter class for your application. You will also learn to validate if your application has started on the local host.

Keywords

  • setup
  • dependencies
  • class
  • starter
  • validation
  • http
  • application
  • testing
  • spring
  • boot

About this video

Author(s)
Tomasz Lelek
First online
24 January 2020
DOI
https://doi.org/10.1007/978-1-4842-5579-7_1
Online ISBN
978-1-4842-5579-7
Publisher
Apress
Copyright information
© Tomasz Lelek 2019

Video Transcript

Welcome in the Spring REST app course. And this is a first segment in which we’ll be doing product setup and starting actual application. So let’s see what you learn in this first segment. At the beginning, we’ll see how to add needed dependencies to Spring Boot project. So we’ll see which dependencies should be added to pom.xml file.

Next we’ll be creating applications starter class. Starter class is an entry point for every Spring Boot application. And it is always needed to be present on your class path. So we’ll define such class for our use case.

Lastly, when we will define the application starter class, we’ll be validating that the HTTP app started on the local host. So we’ll just press Run Keyword, and then we will validate that our application started on the local host 8080 port.

So every Spring Boot application needs to have pom.xm l file. This file defines only the dependencies that you need to have in your application.

So let’s go through this definition of our project and see how does it look like. So at the beginning what is very important, we need to inherit from the parent project. Part of the project is a Spring Boot starter parent. And here we have the newest release of this project.

Then you are defining dependencies. But before that, we need to define the description of our project. So we are specifying that this project should lie within our group ID, artifact ID, and version. We are giving it the name of the REST app. And then there is a description that is inherited from the Spring initilizer.

We’ll go through that initializer in a moment. But first thing, let’s go through the dependencies that we are defining. So first thing we need to define that Java version that we will be using is 1.8. That is important thing.

Then we have all the dependencies related to the Spring Boot starter. So first you are defining Spring starter data JPA. We will need that for persistence. We’ll be doing that in the segment number three. And in that segment, we will define persistence of our entities into database.

Also, we want to expose that via a REST API. That’s why we are adding Spring Boot starter data REST dependency. So that is a second dependency that we need.

Thirdly, we are creating Spring Boot starter web. This is dependency that allows us to create REST controllers. Well, those three dependencies are production dependencies. But also we need to have testing capabilities of our project. That’s why we are adding dependency to the arc springframework.boot Spring Boot starter test. And scope of this dependency should be test because we don’t want to pollute the production class path. It will be present only in the test class path.

Finally, for the integration tests and mocking purpose, we are supplying a meta database that will be automatically picked by Spring Data and used as a back end if we don’t need to connect to the actual real, live database. Also, we have a spring book maven plugin that allows us to build our product and ship it as a one embedded JAR. But this pom.xml could be easily generated for us.

So if you want to generate that, you can go to the Spring Initializer I/O. And here you have a couple interesting things. So we can specify language of the product that you should want, could be Java coding on Groovy. You want to specify version of the Spring Boot, then group ID. So you can type your own group ID. I did this one.

Then application name. It could be demo. And then most important, we can specify all dependencies that we need. So for example, I did web, and it started Spring Web Starter. Also, I have data. So Spring Data JPA, and so on. For example, if you want to add capabilities of reactive programming to your application, you can just type reactive and it will add reactive web to your application.

Once you have that definition ready, you can generate the project by clicking the Generate the project button, and it will create a demo.zip. You can extract that project and import it. But I was supplying the product that we are creating throughout this course for you, so you don’t need to do it. So once we have all dependencies and everything ready, we are ready to create REST application starter. So REST applications starter is just a simple Java class that needs to have two things in it.

First one is Spring Boot application annotation. This is an annotation that encapsulates different annotations, like for example Spring Autoconfiguration, enable Autoconfiguration, or component scan. This is very important for us because, for example, component scan gives the Spring ability to scan whole class path and find out components that could be injected into dependency injection. We’ll be looking at the dependency injection in the next segment. But this is one of the crucial configurations that are needed if you want to enable it.

So once we annotated our Spring Boot application with this annotation, we also need a public static void main method. This method is static arguments. It could be any argument passed by the command line. But for the testing purpose right now, we don’t need anything. And this line is very important. So we are just running REST application and passing those arguments to the Spring REST application. Also, as you can see here, we are specifying property of our active profile.

But for the purpose of this segment, we don’t need to worry about that. We will be analyzing that in the subsequent segments. But right now, let’s focus on this line. So we are running our application with arguments passed. So to do it, you can just do run off your application main method. You can do it from the command line, from the IntelliJ IDE, or any IDE that you want. And after some time, your application will start on the local host.

You can see that there is a log started REST application on the 8080 local host port. So if you will go to that local host, you will see that you are getting links. That you have available application because we have Spring Data REST dependency added. But what’s important, we have application up and running. So we have profiles, for example, and so on. So this is a sign now that your application is up and running.