Java REST API Explained with Spring Boot – Build Web Services
Published: 19 November 2026 | Author: CCI Admin | Category: Java / Full Stack Development
Modern applications communicate with servers to exchange information. For example, when we log in to a website, view student details, or submit an online form, the frontend communicates with the backend through APIs.
One of the most popular ways to build these communication services is using REST APIs. Java developers commonly use the Spring Boot framework to create RESTful web services.
In this blog, let's understand what a REST API is, how it works, HTTP methods, and how to create a simple REST API using Java and Spring Boot with practical examples.
1. What is a REST API?
REST stands for Representational State Transfer. An API (Application Programming Interface) allows two software applications to communicate with each other.
A REST API is a web service that follows REST architectural principles and uses HTTP requests to exchange data between a client and a server.
Most REST APIs use JSON (JavaScript Object Notation) to send and receive data.
Example: Imagine a Student Management System. When a student clicks the View Students button, the frontend sends a request to the Java backend. The backend retrieves student details and returns the information as JSON.
2. How Does a REST API Work?
A REST API generally follows these steps:
- The user performs an action on a website or mobile application.
- The frontend sends an HTTP request to the backend API.
- The Java backend processes the request.
- The backend communicates with the database when necessary.
- The server returns a response, commonly in JSON format.
- The frontend displays the received information.
Simple Architecture:
Frontend (HTML / CSS / JavaScript)
|
v
HTTP Request
|
v
Java REST API
(Spring Boot)
|
v
Database
|
v
JSON Response
|
v
Frontend
3. What is Spring Boot?
Spring Boot is a Java framework used to develop standalone, production-ready applications with minimal configuration.
It simplifies REST API development by providing built-in support for web applications, dependency management, and automatic configuration.
Advantages of Spring Boot:
- Easy application configuration.
- Built-in server support.
- Simple REST API development.
- Database integration support.
- Suitable for enterprise applications.
- Reduces manual configuration.
4. Important HTTP Methods in REST API
REST APIs use different HTTP methods to perform operations on resources.
| HTTP Method | Purpose | Example |
|---|---|---|
| GET | Retrieve data | View students |
| POST | Create new data | Add student |
| PUT | Update existing data | Edit student |
| DELETE | Delete data | Remove student |
5. How to Create a Java REST API Using Spring Boot
Let's create a simple Student Management REST API using Spring Boot.
Step 1: Create a Spring Boot Project
Visit Spring Initializr and create a new project.
Website: https://start.spring.io/
Select the following options:
- Project: Maven
- Language: Java
- Build Tool: Maven
- Packaging: Jar
- Java: 17 or a compatible supported version
- Dependency: Spring Web
Generate and download the project, then open it using IntelliJ IDEA, Eclipse, or another Java IDE.
Step 2: Understand the Project Structure
student-api/ | |-- src/ | |-- main/ | |-- java/ | | |-- com.example.studentapi/ | | |-- StudentApiApplication.java | | |-- Student.java | | |-- StudentController.java | | | |-- resources/ | |-- application.properties | |-- pom.xml
Step 3: Create a Student Class
This class represents student information.
public class Student {
private Long id;
private String name;
private String course;
public Student() {
}
public Student(Long id, String name, String course) {
this.id = id;
this.name = name;
this.course = course;
}
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 getCourse() {
return course;
}
public void setCourse(String course) {
this.course = course;
}
}
Step 4: Create a REST Controller
The @RestController annotation tells Spring that this class handles REST API requests and returns response data.
Create StudentController.java:
import org.springframework.web.bind.annotation.*;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;
@RestController
@RequestMapping("/api/students")
public class StudentController {
private Map<Long, Student> students =
new ConcurrentHashMap<>();
private AtomicLong counter = new AtomicLong();
// GET: View all students
@GetMapping
public List<Student> getStudents() {
return new ArrayList<>(students.values());
}
// POST: Add a new student
@PostMapping
public Student addStudent(
@RequestBody Student student) {
Long id = counter.incrementAndGet();
student.setId(id);
students.put(id, student);
return student;
}
// PUT: Update student
@PutMapping("/{id}")
public Student updateStudent(
@PathVariable Long id,
@RequestBody Student student) {
if (!students.containsKey(id)) {
throw new NoSuchElementException(
"Student not found");
}
student.setId(id);
students.put(id, student);
return student;
}
// DELETE: Remove student
@DeleteMapping("/{id}")
public String deleteStudent(
@PathVariable Long id) {
if (students.remove(id) == null) {
throw new NoSuchElementException(
"Student not found");
}
return "Student deleted successfully";
}
}
Note: This beginner example stores data in memory. Student records will be lost when the application restarts. A real-world application should use a database such as MySQL or PostgreSQL.
6. Understanding Important Spring Boot Annotations
| Annotation | Purpose |
|---|---|
| @RestController | Defines a REST controller. |
| @RequestMapping | Defines a common request URL. |
| @GetMapping | Handles HTTP GET requests. |
| @PostMapping | Handles HTTP POST requests. |
| @PutMapping | Handles HTTP PUT requests. |
| @DeleteMapping | Handles HTTP DELETE requests. |
| @RequestBody | Reads request data from the HTTP body. |
| @PathVariable | Reads values from the URL path. |
7. How to Run the Spring Boot Application
Follow these steps to execute the application:
- Open the project in your Java IDE.
- Run StudentApiApplication.java.
- Wait until the Spring Boot application starts.
- Use Postman or another REST client to test the endpoints.
Base API URL:
http://localhost:8080/api/students
8. Test REST API Using Postman
Postman is a tool used to send HTTP requests and test APIs.
Example 1: GET Request
Method: GET
GET http://localhost:8080/api/students
Example 2: POST Request
Method: POST
Choose Body → raw → JSON and enter:
{
"name": "Arun",
"course": "Java"
}
Example Response:
{
"id": 1,
"name": "Arun",
"course": "Java"
}
Example 3: PUT Request
PUT http://localhost:8080/api/students/1
JSON Body:
{
"name": "Arun Kumar",
"course": "Full Stack Java"
}
Example 4: DELETE Request
DELETE http://localhost:8080/api/students/1
These examples demonstrate how a Java REST API performs basic CRUD operations.
9. Real-World Applications of Java REST APIs
- Student Management Systems: Create, view, update, and delete student details.
- E-commerce Applications: Manage products, orders, and customer information.
- Banking Applications: Connect frontend applications with backend banking services.
- Mobile Applications: Exchange information between Android apps and servers.
- Online Booking Systems: Process bookings and retrieve availability information.
10. REST API Development – Important Skills for Java Developers
Students interested in backend and Full Stack Java development should learn:
- Core Java and OOP concepts.
- Collections and Exception Handling.
- Spring Framework and Spring Boot.
- REST API development.
- MySQL and database connectivity.
- JPA and Hibernate.
- Postman API testing.
- Git and GitHub.
- Basic HTML, CSS, and JavaScript.
11. Frequently Asked Questions (FAQ)
1. What is a REST API in Java?
A REST API in Java is a web service that allows applications to communicate through HTTP requests and exchange information, commonly in JSON format.
2. What is Spring Boot?
Spring Boot is a Java framework that simplifies application development and makes it easier to create standalone applications and REST APIs.
3. What is the difference between GET and POST?
GET is used to retrieve information from a server, whereas POST is commonly used to create new resources by sending data to the server.
4. What is the use of @RestController?
@RestController is a Spring annotation used to create REST API controllers that return response data directly.
5. What is JSON in REST API?
JSON is a lightweight data format used to exchange information between frontend and backend applications.
6. Can Java REST APIs connect to MySQL?
Yes. Java REST APIs developed with Spring Boot can connect to MySQL using Spring Data JPA, JDBC, and other database technologies.
7. What is Postman?
Postman is an API testing tool that allows developers to send HTTP requests and inspect server responses.
12. Practical Activities for Students
Try these mini tasks to improve your REST API development skills:
- Create a simple REST API to display student details.
- Create a POST API to add new student records.
- Develop a PUT API to update student information.
- Create a DELETE API to remove student records.
- Connect your REST API to a MySQL database.
- Build a simple HTML and JavaScript frontend that communicates with your Java REST API.
Conclusion
REST APIs play an important role in modern software development by connecting frontend and backend applications. Java developers can use Spring Boot to create REST APIs quickly and efficiently.
By learning HTTP methods, Spring Boot annotations, JSON, and CRUD operations, students can develop practical backend applications and improve their Full Stack Java development skills.
Start learning Java REST APIs today, build real-world projects, and prepare for your IT career!
Related CCI Blogs
- Introduction to Java Programming
- Java Variables and Data Types
- Java Classes and Objects
- Java Constructors Explained with Simple Examples
- Java Methods – Create Reusable Code with Simple Examples
Learn Java Programming with CCI Computer Education
Want to become a Java developer or Full Stack Developer? CCI Computer Education – Pudukkottai provides practical programming training for students and beginners.
Learn Core Java, OOP concepts, Advanced Java, Spring Boot, REST APIs, SQL, and Full Stack Development through practical exercises and real-world projects.
Explore CCI Computer Education Courses
Contact CCI Computer Education – Pudukkottai
Interested in learning Java and building your IT career? Contact CCI Computer Education for course details and upcoming batches.
Call: 98427 97945 | Website: www.ccipudukkottai.com
WhatsApp Channel: Join CCI WhatsApp Channel
CCI Computer Education – Learn Today, Build Tomorrow.