Build web, including RESTful, applications using Spring MVC. Uses Apache Tomcat as the default embedded container.
The Spring Web Starter dependency is part of the Spring Boot project and is designed to facilitate the development of web applications. When you include the Spring Web Starter dependency in your project, it provides a comprehensive set of libraries and frameworks that are essential for building web applications, including RESTful APIs.
In this tutorial, we will be building StudentTrack to understand how RESTful applications are made using Spring Web. StudentTrack is a simple project designed to manage student records, demonstrating key concepts such as creating REST endpoints, handling HTTP requests and responses, and performing CRUD operations using Spring Boot and Spring Data JPA. Through this hands-on example, you'll gain a solid foundation in developing RESTful web services with the Spring Framework.
As we know Spring Boot Initializer creates a base code template for the application in the /src/main/java folder that is given below:
package com.example.StudentTrack;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class StudentTrackApplication {
public static void main(String[] args) {
SpringApplication.run(StudentTrackApplication.class, args);
}
}
In this code, @SpringBootApplication Annotation automatically set all the configurations required for the application to run whereas
SpringApplication.run method creates an appropriate ApplicationContext instance, configures and registeres all the beans into the Context [Container].
I have saved this file in /Model folder
package com.example.StudentTrack.Model;
import org.springframework.stereotype.Component;
public class Student {
private Long id;
private String name;
private String email;
private String course;
// Constructors
public Student() {}
public Student(Long id, String name, String email, String course) {
this.id = id;
this.name = name;
this.email = email;
this.course = course;
}
// Getters and Setters
public Long getId() { return id; }
public void setId(Long id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getEmail() { return email; }
public void setEmail(String email) { this.email = email; }
public String getCourse() { return course; }
public void setCourse(String course) { this.course = course; }
@Override
public String toString() {
return "Student{id=" + id + ", name='" + name + '\\'' + ", email='" + email + '\\'' + ", course='" + course + '\\'' + '}';
}
}
In this code, @Component registers this class as bean during the initialization of the container or context. Further in the tutorial, we will be learning how to inject this bean as a dependency in any other Component of the application.
Note: For classes that will be managed by Container, should have a default Constructor in its code
we will use @RestController annotation to describe our first Controller.