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 initializer and then use an IDE to create a sample GET route. Therefore, to do this, the following steps are followed:

  1. Go to Spring Initializr
  2. 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

  3. Click on Generate which will download the starter project.
  4. 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.

  5. 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:



    filter_none

    edit
    close

    play_arrow

    link
    brightness_4
    code

    @RestController
    public 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;
        }
    }

    chevron_right

    
    

  6. 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.

  7. 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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : AakashYadav4