DEV Community

Cover image for Understanding REST APIs in Java with Spring Boot: A Beginner-Friendly Guide
Dhananjay Bagul
Dhananjay Bagul

Posted on

Understanding REST APIs in Java with Spring Boot: A Beginner-Friendly Guide

As a Java developer, understanding REST APIs is an important step toward building backend applications. REST APIs allow different applications to communicate with each other and exchange data over HTTP.

In this article, we will understand what REST APIs are, how they work, and how to create a simple REST API using Java and Spring Boot.

  1. What Is a REST API?

REST stands for Representational State Transfer. A REST API allows clients, such as Android applications, web applications, or other services, to communicate with a server using HTTP requests.

For example, an Android HRMS application might need to retrieve employee information from a backend server. The Android application sends a request to a REST API, and the server returns the requested data.

A typical API response may look like this:

{
"id": 1,
"name": "Rahul",
"department": "IT"
}

JSON is commonly used to exchange data between clients and servers.

  1. Common HTTP Methods

REST APIs commonly use the following HTTP methods:

GET — Retrieve data.

POST — Create a new resource.

PUT — Update an existing resource.

PATCH — Partially update a resource.

DELETE — Delete a resource.

For example, an employee management application may use these endpoints:

Method

Endpoint

Purpose

GET

/api/employees

Retrieve employees

GET

/api/employees/1

Retrieve one employee

POST

/api/employees

Create an employee

PUT

/api/employees/1

Update an employee

DELETE

/api/employees/1

Delete an employee

The actual behavior depends on how the backend implements each endpoint.

  1. Why Use Spring Boot?

Spring Boot simplifies Java application development by providing auto-configuration, starter dependencies, and an embedded web server.

For REST API development, the spring-boot-starter-web dependency provides the web framework features needed to create controllers and handle HTTP requests.

You can create a project using Spring Initializr:

https://start.spring.io/

Select Java, choose a compatible Spring Boot version, and add the Spring Web dependency.

  1. Create a Simple REST API

Let's create a basic endpoint that returns a welcome message.

Create a controller class:

package com.example.demo.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class HelloController {

@GetMapping("/api/hello")
public String hello() {
    return "Welcome to my first REST API!";
}
Enter fullscreen mode Exit fullscreen mode

}

Understanding the Code

@RestController

This annotation marks the class as a REST controller. Return values are written to the HTTP response body.

@GetMapping("/api/hello")

This maps HTTP GET requests to the /api/hello endpoint.

hello()

This method returns a simple text response when the endpoint is requested.

Run the Spring Boot application. If it starts successfully on the default port, open:

http://localhost:8080/api/hello

You should see:

Welcome to my first REST API!

  1. Test the API Using Postman

Postman is a useful tool for testing REST APIs without building a frontend first.

Follow these steps:

Open Postman.

Create a new HTTP request.

Select the GET method.

Enter http://localhost:8080/api/hello.

Click Send.

The response body should contain the welcome message.

You can also inspect the HTTP status code, response headers, and request details.

  1. How an Android App Uses a REST API

REST APIs are particularly useful when developing Android applications.

A typical request flow looks like this:

The user opens an Android screen.

The Android application sends an HTTP request to the backend.

The Spring Boot server processes the request.

The server returns a response, often in JSON format.

The Android application reads the response and updates the user interface.

Libraries such as Retrofit can simplify HTTP communication in Android applications.

For example, an HRMS mobile application can use REST APIs to retrieve employee profiles, attendance information, or other employee-related data.

  1. Best Practices for REST API Development

Keep these principles in mind when developing backend APIs:

Use clear and consistent endpoint names.

Choose suitable HTTP methods and status codes.

Validate incoming request data.

Handle errors with meaningful responses.

Keep controller and business logic appropriately separated.

Avoid exposing passwords, private information, or internal server details.

Test successful requests and failure scenarios with Postman.

Use authentication and authorization for protected resources.

For larger applications, a layered architecture with controller, service, and repository components can help keep the code organized and maintainable.

Conclusion

REST APIs are an essential part of modern backend development. Java and Spring Boot provide a practical foundation for creating APIs that connect Android applications, web applications, databases, and other services.

Start with simple GET endpoints, learn how POST and PUT requests work, and then practise building CRUD APIs with proper validation, database integration, and error handling.

Consistent practice with Java, Spring Boot, REST APIs, MySQL, and Postman can help you develop the skills needed to build real-world backend applications.

java #springboot #restapi #beginners #backend

Top comments (1)

Collapse
 
suppdevbot profile image
DEV SUPPORTS •

You need to verify your account.

Enter fullscreen mode Exit fullscreen mode

tr.ee/dev-to