How to create a basic application in Java Spring Boot
Spring MVC is a widely used module of spring which is used to create scalable web applications. But the main disadvantage of spring projects is that configuration is really time-consuming and can be a bit overwhelming for the new developers. Making the application production-ready takes some time if you are new to the spring. The solution to this is Spring Boot. Spring Boot is built on the top of the spring and contains all the features of spring. In this article, we will see how to create a basic spring boot application.
Spring Initializr is a web-based tool using which we can easily generate the structure of the Spring Boot project. It also provides various different features for the projects expressed in a metadata model. This model allows us to configure the list of dependencies which are supported by JVM. Here, we will create the structure of an application using spring initializr and then use an IDE to create a sample GET route. Therefore, to do this, the following steps are followed:
- Go to Spring Initializr
- Fill in the details as per the requirements. For this application:
Spring Boot: 2.2.8
Dependencies: Spring Web
- Click on Generate which will download the starter project.
- Extract the zip file. Now open a suitable IDE and then go to File->New->Project from existing sources->Spring-boot-app and select pom.xml. Click on import changes on prompt and wait for the project to sync.
- Note: In the Import Project for Maven window, make sure you choose the same version of JDK which you selected while creating the project.
- Go to src->main->java->com.gfg.Spring.boot.app, create a java class with name
as Controller and add the annotation @RestController. Now create a GET API as shown below:
- This application is now ready to run. Run the SpringBootAppApplication class and wait for the Tomcat server to start.
- Note: The default port of the Tomcat server is 8080 and can be changed in the application.properties file.
- Now go to the browser and enter the URL localhost:8080. Observe the output and now do the same for localhost:8080/gfg
On running the above application, the following output is generated:
Conclusion: By following the above steps, we have created a simple RESTful route with some message in it. In order to make a more complex application, more RESTful routes are added to perform the CRUD operations on the server.