REST api (Spring Boot)
What is a RESTful API?
Why do we need RESTful APIs?
REST CRUD Operations
Example:
What is an HTTP Request?
Example:
HTTP Response
What is JSON?
What is Postman?
What is Spring Boot?
Installing Spring Boot (EASIEST WAY)
Installing Spring Boot (EASIEST WAY)
Project Structure (Spring Boot)
Basic Spring Boot REST Syntax
Key annotations:
application.properties (DB config)
Running the App
Testing API
Spring Boot doc
70.50K

rest

1. REST api (Spring Boot)

2. What is a RESTful API?

REST = Representational State Transfer
A RESTful API is a way for applications to communicate over the
internet using HTTP.
In simple words:
A REST API allows clients (browser, mobile app, Postman) to talk to
a server and access data.
Example:
Client asks:
GET /api/books
Server responds:
[
{ "id": 1, "name": "Java Book", "price": 20 }
]

3. Why do we need RESTful APIs?

Before (Console apps):
Only work locally
No web integration
Hard to scale
With REST APIs:
Works over internet
Multi-platform
Clean architecture
Industry standard
Easy integration

4. REST CRUD Operations

Operation
HTTP Method
Meaning
Create
POST
Add new data
Read
GET
Fetch data
Update
PUT
Modify data
Delete
DELETE
Remove data

5. Example:

POST /api/books
→ create
GET /api/books
→ read
PUT /api/books/1 → update
DELETE /api/books/1 → delete

6. What is an HTTP Request?

HTTP is the communication protocol between client and server.
An HTTP request contains:
• Method (GET, POST, PUT, DELETE)
• URL
• Headers
• Optional Body

7. Example:

POST
http://localhost:8080/api/books
Body:
{
"name": "Java",
"price": 20
}

8. HTTP Response

Server sends back:
• Status code
• Data (JSON)
Code
Meaning
200
OK
201
Created
400
Bad
Request
404
Not Found
500
Server
Error

9. What is JSON?

JSON = JavaScript Object Notation
A lightweight data format for exchanging information.
Looks like Java object but text-based:
{
"id": 1,
"name": "Java Book",
"price": 20.5
}
JSON Array Example:
[
{ "id": 1, "name": "Java" },
{ "id": 2, "name": "Python" }
]

10. What is Postman?

Postman is a tool to test REST APIs.
With Postman you can:
• Send GET, POST, PUT, DELETE
• Send JSON data
• View responses
• Debug APIs
Example Postman Flow:
1. Select method (POST)
2. Enter URL
3. Add JSON body
4. Click Send
5. View response

11. What is Spring Boot?

Spring Boot is a Java framework for building REST APIs easily.
It provides:
• Embedded server (Tomcat)
• REST annotations
• Dependency management
• Clean architecture

12. Installing Spring Boot (EASIEST WAY)

Option 1 (Recommended): Using IntelliJ IDEA
Steps:
1. Open IntelliJ
2. New Project
3. Spring Initializr
4. Select:
• Project: Maven
• Language: Java
• Spring Boot version (default)
5. Add dependencies:
• Spring Web
• JDBC or JPA
6. Click Create

13. Installing Spring Boot (EASIEST WAY)

Option 2: Spring Initializr Website
https://start.spring.io
(download project zip)

14. Project Structure (Spring Boot)

src/main/java
├── controller
├── service
├── repository
├── model
├── exception
└── Application.java

15. Basic Spring Boot REST Syntax

Controller example:
@RestController
@RequestMapping("/api/books")
public class BookController {
@GetMapping
public List<Book> getAll() {
return service.getAll();
}
@PostMapping
public void create(@RequestBody Book book) {
service.create(book);
}
}

16. Key annotations:

Annotation
Meaning
@RestController
REST class
@GetMapping
GET endpoint
@PostMapping
POST endpoint
@PutMapping
UPDATE
@DeleteMapping
DELETE
@RequestBody
JSON input
@PathVariable
URL parameter

17. application.properties (DB config)

spring.datasource.url=jdbc:mysql://localhost:3306/librar
y
spring.datasource.username=root
spring.datasource.password=1234

18. Running the App

In IntelliJ:
Click Run
OR terminal:
mvn spring-boot:run
Output:
Started Application on port 8080

19. Testing API

Open Postman
Try:
GET http://localhost:8080/api/

20. Spring Boot doc

Github link

21.

import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/api/books")
public class BookController {
REST Controller Example
private BookService service;
public BookController(BookService service) {
this.service = service;
}
// GET all
@GetMapping
public List<BookBase> getBooks() {
return service.getAllBooks();
}
// GET by id
@GetMapping("/{id}")
public BookBase getBook(@PathVariable int id) {
return service.getBook(id);
}
// POST create
@PostMapping
public void createBook(@RequestBody BookBase
book) {
service.createBook(book);
}
// PUT update
@PutMapping("/{id}")
public void updateBook(
@PathVariable int id,
@RequestBody BookBase book) {
service.updateBook(id, book);
}
// DELETE
@DeleteMapping("/{id}")
public void deleteBook(@PathVariable int id) {
service.deleteBook(id);
}
}
English     Русский Rules