Spring is an open source JAVA framework which is widely popular amongst web development as it helps in creating high performing, easily testable and reusable code. Spring Boot is provides the power of Spring with minimal configurations as it covers most of the configurations on it’s own but if one wants custom configurations then that can also be done by extending desired configuration class. I won’t go deep into Spring tutorial rather will focus on developing a web application with Spring Boot.
Following are the prerequisites of the tutorial:
- Java 11
- Spring tools by https://spring.io/tools or you can use IntelliJ idea
- I will be using Apache Maven installed.
First let’s create a project using the New Maven Project wizard in STS
- Step 1:
- Step 2:
- Step 3:
This will create our project and basic required folders and files including the
pom.xml which will contain all the maven configuration of our project.
As you can see, I’ve added
spring-boot-starter-parent as parent maven dependency.
This helps in setting up versions of other child dependencies and makes our project a child project of this parent. So as I’m developing a web application so a new dependency
spring-boot-starter-web needs to be added to get all the required dependencies to create a spring web application.
pom.xml will look something like this:
Here, I’ve added
spring-boot-starter-web dependency without mentioning
version tag as the version of this dependency has been taken care of by parent
spring-boot-starter-parent . So my
pom.xml will look like this.
Now let’s create a JAVA class which will be the initializing point of our application and as this will be our main class so need to create
main method in the class.
main method will make sure this application can stand-alone. So, I’ve added the code inside the main method which tells spring that this is a spring application, to create and start a servlet container, and host this application.
Adding the annotation
@SpringBootApplication and the
main method makes this application stand-alone. This is it! A very simple spring boot application is ready to run. Now, after running the application as spring boot, you’ll notice the following in the console.
This means our application has started in tomcat server on 8080 port which is a default port for tomcat. Now let’s hit the url
http://localhost:8080/ and you’ll notice below screen.
This doesn’t mean our application has failed rather this mean our application is up & running but this error is coming due to missing page while accessing the
http://localhost:8080/ url and so it tried to find page on
/error path which is again missing as I’ve not created any webpage yet so being redirected to this fallback page.
Download the project from GitHub: prafullsranjan/simple-spring-boot (github.com)