How to Create Spring Boot Application Step by Step

Spring Boot had been built for Rapid Application Development. In this tutorial we will learn How to Create Spring Boot Application using some easy examples. We will also focus on How to deploy spring boot application in different application servers.

Create Spring Boot Application using start.spring.io

The best way of bootstrapping Spring Boot application is by using Spring Initializr. Let’s first start by opening http//start.spring.io.As you already know, you can use either Maven or Gradle to build, however we will use Maven in this tutorial. Click on Switch to the full Version and you will be able to see the below screen.

Software Needed

JDK 1.8 (You can use 1.7 or 1.6)

Maven 3.x

Steps :

Select Maven Project

If you want, change the Group/Package name. We will use com.adeveloperdiary

Change the Artifact to SpringBoot, Name will automatically change to the same

Leave the packaging to Jar, Java Version to 1.8 (Make sure you have JDK 1.8 in your local or use 1.7 )

Either type the Dependencies or select them below. We will add Web and Actuator here

Click on Generate Project button to download the zip file

Now Extract the zip and you will have the following files.

Open Terminal and navigate to the above folder. Type mvn clean package and press enter. The Build will start now.

Once the build has been completed, you will have the SpringBoot-0.0.1-SNAPSHOT.jar created inside the target folder.

The Size of the jar would be around 13MB since it will have the embedded Tomcat server in it.

Create Spring Boot Application using Eclipse

We will use STS Toolkit to bootstrap when using eclipse. I am using eclipse MARS .1 version. We will first install the STS (SpringSource Tools Suite). Open Eclipse Marketplace, Type STS in Find and install the STS.

Then open Spring perspective.

Create a New -> Spring Starter Project.

Enter the following details. Click Next.

Select Web and Actuator. Click on Next.

Click on Finish.

This should create the Spring Boot Project in Eclipse.

Create Spring Boot Application using IntelliJ IDEA

We will now use IntelliJ IDEA to create our Spring Boot Application.I recommend to use the latest version of IntelliJ. Create a New Project. Select Spring Initializr. Click on Next.

Enter the following details. Click Next.

Select Web and Actuator. Click Next.

Enter the project Name and click Finish.

Your Spring Boot Project has been created now.

Run Spring Boot Application using CLI

We had created the jar file, now we will run it from Command Line by executing this line

1

java-jar target/SpringBoot-0.0.1-SNAPSHOT.jar

You should be able to see Spring Boot in the Command Line.

The server should have started and the default port number will also be displayed as below.

Run Spring Boot Application in Eclipse

Its much easier to run the Spring Boot app in eclipse. Select the project, Right Click and, move to Run As and click on Spring Boot App. Again, remember you need to select the project to get this option, not the pom file.

Here is the output.

Run Spring Boot Application in IntelliJ IDEA

Its probably most easy, click on the run icon.

You should be able to see the logs that the application has started.

Conclusion

Hope this Step by Step approach will help you. Next we will learn how to create RESTServices and use Actuator in Spring Boot. Later we will also learn how to deploy Spring Boot Application in IBM Liberty Profile and IBM WAS 8.5 Server.

This is the 2nd part of Getting Started with Spring Boot Series. Find all the parts here.

Related

Comments

If I run a project more than once on the same port with STS, it gives me an error that the port is already in use. If I change the port again, it is working for the first time only. Can you please help me on this? Should I stop and start the server again and again for every code change?

The application is still running at the background. You can restart the server, that should resolve the problem. If you are running it as standalone jar make sure that its stopped while you try to run it again.

In case this does not help, pleas let me know what type of server you are using and how you are running it (IDE, Server Name, Steps etc).

hi … i have installed STS from eclipse market place..i am using Eclipse Luna.
When i create a new project-> spring starter project
It is giving me error that M2e connector is not installed. before Installing STS , maven was there, but after installing it just vanished frm my system..
I hv even downloaded m2e connectors from market place.. stl no success.
please help

However, it keeps displaying BUILD FAILURE
Total time: 0.059 s
[INFO] Finished at: 2016-12-07T11:26:14-05:00
[INFO] Final Memory: 6M/310M
[INFO] ————————————————————————
[ERROR] The goal you specified requires a project to execute but there is no POM in this directory (C:\Downloads\SpringBoot\src\main\java\com\adeveloperdiary). Please verify you invoked Maven from the correct directory.