This is a short guide about how to start your first Spring Boot Application.
Hi all! This guide provides a sample of creating your application with Spring Boot!
Spring Boot is the Java Enterprise Framework that makes it easy to create Spring powered applications that are stand-alone, production-ready and to create it with a minimum of efforts.
At this point:
– stand-alone means that application does not load any external module, library function or program.
– production-ready means that the application you built is not a simple Hello world app, but the fully developed app that is ready to be released live to users.
– minimum of efforts— is just to tell you that creation of java based application with Spring Boot, that is ready to go — requires a minimum of your efforts and time.
In this guide you’ll figure out how to create the Spring Boot application using Maven.
So, let’s dive in it and check it out!
To start we’ll need to create a simple Java project. For this guide I’ll be using the Intellij Idea from JetBrains. So, go to the File>New>Project. Choose Java and click Next button. Idea may suggest you to create the project from template, but ignore it, and click Next one more time.
On the next screen you’ll be able to set up the project name. Let’s call it spring_boot_app.
Ok. The project is created, so let’s make it to be a Maven project. You can simply do that by right mouse click on the root of the project folder (“spring_boot_app” folder) and choose “Add Framework Support…”. It will open up new pop-up where you can choose what framework you exactly want to get. So, scroll it to the bottom and choose “Maven” and click OK. This step will transform your Java project into Maven one.
Now you will have one new important file called pom.xml. It is an XML file that contains information about the project and configuration details used by Maven to build the it. Also, you will see small pop-up saying “Maven projects need to be imported”. I would suggest you to click “Enable Auto-import” in order to allow Maven to import the dependencies you specify in your pom.xml automatically for you.
Moving forward, I would say that the pom.xml is the only xml file you need to setup and run the Spring Boot Application.
Ok, in order to make the project to be a Spring Boot, you’ll have to add Spring support. The very popular way is to inherit the parent pom.xml file that contains all common used libraries for Spring projects.
In simple words — guys from Spring were gathering information and analytics for a long time about what is the most used when people develop Spring based application. And they figured out that there is some default set of libraries that are used by developers to create it. And they decided to make some top-level dependencies that will contain all of those default dependencies, so developers won’t need to spend time by importing those smaller dependencies one by one. So, they made our life easier. And that top-level dependency called spring-boot-starter-parent.
Now, when you specified the parent pom.xml file, it will automatically get some default jars to your Spring Boot project.
By default Maven uses java 1.6 version and of course you may use java 8 by adding <properties> block and specify the <java.version> to be 1.8.
Let’s move forward. As we are building web application, we’ll need all the libraries for developing web applications. And again, Spring Boot has something for us to offer. The dependency we’ll have to add called spring-boot-starter-web. This is also a top-level dependency and it contains the most common used libraries for building web applications using Java. There are such jars in it as :
- Spring beans
- Spring context
- Spring core
- Spring aop
- Spring webmvc
- Tom cat
Yes! It has embedded Tomcat server, so you don’t need to download and install it yourself. That is already done for you! Amazing, right?
Ok, so this is how your pom.xml should look like for now.
And I have to let you know that we are done with pom.xml. Absolutely. I’m not kidding.
You remember how much stuff you had to add yourself to import all the required dependencies just to be able to run the project? This is in the past. Now you can just add this one dependency in order to be able to start your application.
Of course, this is just a simple application, that will only be able to boot and will not do anything more, and in order to add some functionality to your app you’ll have to inject other dependencies, and we’ll talk about it in other guides, but not now. Today we want just to run it and to learn what we need to do in order to just run it.
So, our pom.xml is ready. Let’s add some code to be able to boot our app. Under the java folder let’s create the “pac” folder that will contain all our source code.
The main class that will be the start point for our application we’ll put under this “pac” folder. I’ll call it “Application”, you may choose any name you want.
Many Spring Boot developers always have their main class annotated with @Configuration, @EnableAutoConfiguration and @ComponentScan. Since these annotations are so frequently used together (especially if you follow the best practices), Spring Boot provides a convenient @SpringBootApplication alternative. The @SpringBootApplicationannotation is equivalent to using @Configuration, @EnableAutoConfiguration and @ComponentScan with their default attributes.
We’ll have to add public void main method in this class, because it will be the engine of the app, it’s start point.
The SpringApplication class provides a convenient way to bootstrap a Spring application that will be started from a main() method. You can just delegate to the static SpringApplication.run method:
And in order to check that it works, I’ll create the controller that will return some string value to for us to be able to see that the server is running.
Here I created package with name “package_with_my_controller” under the our main package with name “pac”. Added a java class called “MyController”. Annotated this class with @RestController annotation, so the Spring will know that this is our controller. Added method “homePage” that will be triggered once the url “/” is called, and will return the string with value “Hello, I’m SPRING BOOT and I am running!”.
So, let’s test it. Go to the top right corner of Idea, click on the small triangle pointing down, choose “Edit Configuration”.
At the top left corner of the pop-up click on green plus sign, and scroll to Spring Boot and click OK. Then at the top of that pop-up you should point at your Main class.
When you’re done, click OK.
And let’s start it.
If you followed me step by step you’ll see such logs:
Now we’ll have to open our web browser and go to the address https://localhost:8080. The port 8080 is the default for Tomcat.
Here we go!
As we expected the server returns “Hello, I’m SPRING BOOT and I am running!”. So we did it!
Our Spring Boot app is running.
That’s all folks! Thanks for your attention! If you have any feedback or question feel free to share your thoughts.