Creating your first Spring boot Application
Initially before beginning to create our first Spring boot application let me give you an introduction to Spring boot.
What is Spring boot?
Spring boot is an open-source framework based on Java used to create a microservice ( It is an architecture that allows developers to develop and deploy services independently ). Spring boot is used to build stand-alone and production-ready Spring applications that you can run so easily. Spring boot is built on top of the Spring framework and it makes setting up, configuring, and running both simple and web-based applications easier and quicker. Simply Spring boot is a combination of Spring framework and embedded servers.
Requirements
· Java 1.8
· Gradle 4+ or Maven 3.2+
· A favourite IDE ( Spring tool Suite is recommended)
How to create your first project?
I will be using Spring Initializr to generate my Spring boot project with the required dependencies.
When you visit the above link by default everything will be selected. You can select the project type ( Maven or Gradle ) you require. In the dependencies section, you can select the type of Spring boot application you are going to build. Here, I have selected Spring Web as the dependency.
So after you have done the necessary changes you can click generate to download your Spring boot project. After the project has downloaded open it in your favourite IDE.
Creating a simple web application
Now let's create a controller for the application. Go to the following directory within the project and create a new java file as WebController.java
src/main/java/com/example/springboot/WebController.java
Type the following code in the WebController.java file.
@RestController annotation is used to imply that this application is to use by Spring MVC to handle web requests. @RequestMapping maps the method to the specified path in the URL.
Now we have successfully created our first Spring boot application. So we can now run and check whether it runs successfully.
Run the Application
To run the application open the spring boot project folder in the command prompt and type the following.
If you use maven type,
./mvnw spring-boot:run
If you use Gradle type,
./gradlew bootRun
After you run the above command if you see the Spring logo as above that means you have successfully deployed your Spring boot application.
Now let's check it in the browser. Open your favourite browser and go to localhost:8080
Congratulations, on your first Spring boot project.