How to create a basic application in Java Spring Boot

28 Jan, 2022
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 InitializrFill in the details as per the requirements. For this application:  
Go to Spring Initializr
Fill in the details as per the requirements. For this application:  
Project: Maven Language: Java Spring Boot: 2.2.8 Packaging: JAR Java: 8 Dependencies: Spring Web  
Click on Generate which will download the starter project.  
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.  
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: 
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: 
Java
@RestControllerpublic class Controller {     // One syntax to implement a    // GET method    @GetMapping("/")    public String home()    {        String str            = "<html><body><font color=\"green\">"              + "<h1>WELCOME To GeeksForGeeks</h1>"              + "</font></body></html>";        return str;    }     // Another syntax to implement a    // GET method    @RequestMapping(        method = { RequestMethod.GET },        value = { "/gfg" })     public String info()    {        String str2            = "<html><body><font color=\"green\">"              + "<h2>GeeksForGeeks is a Computer"              + " Science portal for Geeks. "              + "This portal has been "              + "created to provide well written, "              + "well thought and well explained "              + "solutions for selected questions."              + "</h2></font></body></html>";        return str2;    }}
This application is now ready to run. Run the SpringBootAppApplication class and wait for the Tomcat server to start. 
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
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
Output: 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.
AakashYadav4
germanshephered48
java-advanced
Java-Spring
Web-Programs
Java Programs
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Iterate Over the Characters of a String in Java
How to Convert Char to String in Java?
How to Get Elements By Index from HashSet in Java?
Java Program to Write into a File
How to Write Data into Excel Sheet using Java?
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Installation of Node.js on Linux
Difference between var, let and const keywords in JavaScript
How to insert spaces/tabs in text using HTML/CSS?
How to fetch data from an API in ReactJS ?