How to Create a Project in Spring Boot?

Spring Boot is a framework that is built on top of the Spring Framework in order to make application development faster and easier.

Spring Boot is designed to automatically configure Spring whenever possible and therefore Spring Boot applications need very little Spring configuration.

How Spring Boot Works

Spring Boot uses your classpath and beans that you have configured to add items that your application is missing. With Spring Boot, a developer can focus more on business features and less on infrastructure.

Sample Project in Spring Boot

To create a project in Spring Boot from scratch, do the following:

  1. Go to Spring Initializr at https://start.spring.io and fill in your project details as follows:
    • Project: Choose Gradle Project or Maven Project as per your project requirement.
    • Language: Java
    • Spring Boot: Latest stable version of Spring Boot is selected by default. So leave it as is.
    • Project Metadata: Provide group name in the Group field. The group name is the id of the project. In Artifact field, provide the name of your project. In the package field, provide package name for your project. Next, select your preferred version of Java that is installed on your computer and is available on your local environment.
    • Dependencies: Spring provides a number of Spring Starters that allows to add correct compatible version of jars in the classpath of your application. Add all dependencies that your project needs. Dependencies which are not available as Spring Starters can be added later from the project management file (pom.xml for Maven projects and build.gradle file for Gradle projects). In the image below, you'll see that we are creating a new Spring Boot project with dependencies for Spring Web which is needed for building a Spring web application, and Spring Boot DevTools which is needed for quick restart of the application while it is running:
  2. Click the GENERATE button and save/download the project zip bundle.
  3. Extract the project to your preferred working directory.
  4. Import the project in your preferred Java development IDE such as Eclipse or IntelliJ IDEA.
  5. Open src/main/resources/application.properties file and add the following configuration:
  6. 
        #port for the application to run 
        server.port = 8080
        
  7. Create a getter/setter Student class with the following fields:
  8. 
    package com.example.model;
    
    public class Student {
    
    	private String firstName;
    	private String lastName;
    	private String email;
    	private String contactNumber;
    
    	public String getFirstName() {
    		return firstName;
    	}
    
    	public void setFirstName(String firstName) {
    		this.firstName = firstName;
    	}
    
    	public String getLastName() {
    		return lastName;
    	}
    
    	public void setLastName(String lastName) {
    		this.lastName = lastName;
    	}
    
    	public String getEmail() {
    		return email;
    	}
    
    	public void setEmail(String email) {
    		this.email = email;
    	}
    
    	public String getContactNumber() {
    		return contactNumber;
    	}
    
    	public void setContactNumber(String contactNumber) {
    		this.contactNumber = contactNumber;
    	}
    
    }
    
  9. Next, create a controller class with some REST APIs as shown in the example below:
  10. 
    package com.example.controller;
    
    import org.springframework.http.ResponseEntity;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestBody;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class StudentController {
    
    	@GetMapping(path = "/students")
    	public ResponseEntity<Student> getStudent() {
    
    		Student student = new Student();
    		student.setFirstName("Tom");
    		student.setLastName("A");
    		student.setEmail("[email protected]");
    		student.setContactNumber("5xxxxxxx03");
    
    		return ResponseEntity.ok(student);
    	}
    
    	@GetMapping(path = "/add-students")
    	public ResponseEntity<Student> addStudent(@RequestBody  Student student) {
    		// code to save student here
    
    		return ResponseEntity.ok(student);
    	}
    }
    
  11. There should be src/main/java/YourApplicationMain.java main class file that you can right-click and run to choose as Java application to run your Spring Boot project.
  12. The above two REST APIs should be accessible at http://localhost:8080/student and http://localhost:8080/add-student.

Congratulations, you have learned how to create a simple Spring Boot Project.