0% found this document useful (0 votes)
2 views10 pages

Restful API Java

This document serves as a comprehensive guide to building RESTful APIs using Spring Boot, emphasizing key concepts such as Dependency Injection, Autoconfiguration, and the Controller-Service-Repository architecture. It outlines the advantages of Spring Boot over traditional Spring Framework, including reduced configuration and the use of Starters for dependency management. The document also provides a structured learning path for developers new to REST API development with Spring Boot.

Uploaded by

Fernand Niyirema
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views10 pages

Restful API Java

This document serves as a comprehensive guide to building RESTful APIs using Spring Boot, emphasizing key concepts such as Dependency Injection, Autoconfiguration, and the Controller-Service-Repository architecture. It outlines the advantages of Spring Boot over traditional Spring Framework, including reduced configuration and the use of Starters for dependency management. The document also provides a structured learning path for developers new to REST API development with Spring Boot.

Uploaded by

Fernand Niyirema
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Building RESTful APIs

with Spring Boot


Dependency Injection, Autoconfiguration, Starters,
and the Controller–Service–Repository Architecture

Java & Spring — Reference Guide


June 23, 2026
Java & Spring Spring Boot — REST APIs

Contents

1 The Big Picture 2

2 What Is the Spring Framework? 2

3 The Most Important Concept: Dependency Injection 3

4 What Is the Spring Container? 3

5 What Spring Boot Adds 4

6 Autoconfiguration 4

7 What Is an “Opinionated Approach”? 4

8 What Are Starters? 5

9 What Is Spring Initializr? 6

10 Embedded Server 6

11 Typical REST API Architecture 6


11.1 Controller Layer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
11.2 Service Layer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
11.3 Repository Layer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
11.4 Database Layer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

12 Why Spring Boot Is So Popular for APIs 8

13 What You Must Learn First 8

14 Mental Model to Remember 9

June 23, 2026 1


Java & Spring Spring Boot — REST APIs

1. The Big Picture


The goal is to build endpoints like:
GET / students
POST / students
PUT / students /1
DELETE / students /1

Without Spring Boot


• Create project structure manually
• Download dependencies manually
• Configure web server manually
• Configure database connection manually
• Configure JSON conversion manually
• Configure security manually
• Configure dependency injection manually
Result: a lot of setup before writing any real code.

With Spring Boot


Create project
Write code
Run application

Spring Boot handles most configuration automatically.


Mental model:
• Java = the language
• Spring Framework = a powerful toolbox
• Spring Boot = an automatic toolbox installer

2. What Is the Spring Framework?


Before Spring Boot existed, developers used the Spring Framework, which provides:
• Dependency Injection (DI)
• MVC Architecture
• Database Access
• Security
• Validation
• Testing

June 23, 2026 2


Java & Spring Spring Boot — REST APIs

and many other features.

3. The Most Important Concept: Dependency Injection


This is the heart of Spring.

The Problem
public class StudentService {

StudentRepository repository =
new StudentRepository () ;
}

StudentService creates its own repository — the classes become tightly coupled.

The Spring Way


Spring says: “Don’t create your dependencies yourself.”
@Service
public class StudentService {

private final StudentRepository repository ;

public StudentService ( StudentRepository repository ) {


this . repository = repository ;
}
}

Spring automatically creates StudentRepository and injects it. This is called Depen-
dency Injection (DI).
Why is it useful? The service does not care how the repository is created. This
makes code cleaner, easier to test, and easier to replace.

4. What Is the Spring Container?


Spring has a special object manager called the IoC Container (IoC = Inversion of
Control).

Its job
Create objects
Store objects
Inject objects
Manage object lifecycle

Instead of:

June 23, 2026 3


Java & Spring Spring Boot — REST APIs

new StudentService ()
new StudentRepository ()

Spring creates them automatically and gives them to you.

5. What Spring Boot Adds


Spring Framework is powerful, but it requires lots of configuration. Spring Boot removes
most of that configuration. It mainly provides:
1. Autoconfiguration
2. Starters
3. Embedded Servers

6. Autoconfiguration
Suppose Spring Boot sees spring-boot-starter-web in your project. Spring Boot
thinks: “Oh, this is a web application.” Then it automatically:
• Configures Tomcat
• Configures Spring MVC
• Configures JSON conversion
• Configures REST support
without you writing configuration files.

Example
@RestController
public class HelloController {

@GetMapping ( " / hello " )


public String hello () {
return " Hello " ;
}
}

And it works — no server setup, no XML, no web configuration.

7. What Is an “Opinionated Approach”?


This phrase scares many beginners. It simply means: “Spring Boot makes reasonable
decisions for you.”
Instead of asking:

June 23, 2026 4


Java & Spring Spring Boot — REST APIs

Which JSON library ?


Which web server ?
Which version ?
Which settings ?

Spring Boot says: “I’ll choose the standard ones.” Usually:


• Tomcat
• Jackson JSON
• Spring MVC
You can change them later if needed, but beginners don’t need to.

8. What Are Starters?


This is extremely important. A starter is a package containing many dependencies.
Instead of installing 20 libraries:
spring - boot - starter - web

installs everything needed for web APIs.

Popular Starters
Spring Web
spring - boot - starter - web

Provides: REST APIs, Controllers, JSON conversion, embedded Tomcat.

Spring Data JPA


spring - boot - starter - data - jpa

Provides: database access, repositories, ORM with Hibernate.

Spring Security
spring - boot - starter - security

Provides: authentication, authorization, login system.

Validation
spring - boot - starter - validation

Provides input validation, e.g.:

June 23, 2026 5


Java & Spring Spring Boot — REST APIs

@NotNull
@Email
@Size ( min =3)

9. What Is Spring Initializr?


When creating a Spring Boot project, you usually visit Spring Initializr. You choose:
• Project Name
• Java Version
• Spring Boot Version
• Dependencies
Example selection:
Spring Web
Spring Data JPA
MySQL Driver
Lombok

Click Generate — Spring Boot creates the project.

10. Embedded Server


Old Java Web Development
Build application
Deploy WAR file
Install Tomcat separately

A lot of work.

Spring Boot
Spring Boot includes Tomcat inside your project. When you run:
mvn spring - boot : run

or
java - jar app . jar

Tomcat starts automatically. That’s why Spring Boot applications are called standalone
applications.

11. Typical REST API Architecture


You’ll use this structure almost every time:

June 23, 2026 6


Java & Spring Spring Boot — REST APIs

Controller
|
Service
|
Repository
|
Database

11.1. Controller Layer


Receives HTTP requests.
@RestController
@RequestMapping ( " / students " )
public class StudentController {

Examples: GET /students, POST /students.

11.2. Service Layer


Business logic.
@Service
public class StudentService {

Examples: calculate grades, validate business rules, process data.

11.3. Repository Layer


Database communication.
@Repository
public interface StudentRepository
extends JpaRepository < Student , Long > {

Examples: save student, delete student, find student.

11.4. Database Layer


MySQL
PostgreSQL
Oracle
SQL Server

June 23, 2026 7


Java & Spring Spring Boot — REST APIs

12. Why Spring Boot Is So Popular for APIs


A complete REST API can be created with surprisingly little code.

Entity
@Entity
public class Student {
private Long id ;
private String name ;
}

Repository
public interface StudentRepository
extends JpaRepository < Student , Long > {
}

Controller
@GetMapping ( " / students " )
public List < Student > getStudents () {
return repository . findAll () ;
}

Done. Spring Boot automatically converts Java objects to JSON, creates HTTP
responses, handles requests, and connects the layers.

13. What You Must Learn First


For REST API development, learn in this order:

Step 1 — Java OOP


Classes, Objects, Inheritance, Interfaces.

Step 2 — Maven or Gradle


Dependency management.

Step 3 — Spring Core


Dependency Injection, IoC Container, Beans.

Step 4 — Spring Boot Basics


Starters, Autoconfiguration, [Link].

June 23, 2026 8


Java & Spring Spring Boot — REST APIs

Step 5 — REST APIs


GET, POST, PUT, DELETE, PATCH.

Step 6 — Spring Data JPA


Entity, Repository, Relationships, Queries.

Step 7 — Validation
@NotNull
@Email
@Size

Step 8 — Exception Handling


@ControllerAdvice

Step 9 — Spring Security + JWT


Authentication and authorization.

14. Mental Model to Remember


Whenever you build a Spring Boot REST API, think:
Client ( Postman / Frontend )
|
Controller
|
Service
|
Repository
|
Database

• Spring Framework = the engine


• Spring Boot = the automatic setup around the engine
• Dependency Injection = Spring creates objects for you
• Starters = dependency bundles
• Autoconfiguration = automatic setup
• Embedded Tomcat = built-in web server

Once you fully understand those five ideas, about 70% of beginner Spring Boot
confusion disappears, and concepts like @RestController, @Service, @Repository,
@Autowired, JpaRepository, and REST endpoints start making much more sense.

June 23, 2026 9

You might also like