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

Lab05 SpringBoot REST API SectionB

The document outlines Lab 03 of the Web Architecture course, focusing on building a REST API using Spring Boot for managing a book catalog. It details the lab objectives, prerequisites, and the three-layer architecture of the application, including the roles of the Controller, Service, and Repository. Additionally, it provides instructions for testing the API, completing guided extensions, and submitting the final project with specific assignment requirements.
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 views8 pages

Lab05 SpringBoot REST API SectionB

The document outlines Lab 03 of the Web Architecture course, focusing on building a REST API using Spring Boot for managing a book catalog. It details the lab objectives, prerequisites, and the three-layer architecture of the application, including the roles of the Controller, Service, and Repository. Additionally, it provides instructions for testing the API, completing guided extensions, and submitting the final project with specific assignment requirements.
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

CSE 4636 – Web Architecture Lab Lab 02: Servlets, Sessions

WEB ARCHITECTURE LAB 03

Spring Boot REST API — IUT Library API

Prepared by:
Njayou Youssouf
Lecturer, IUT CSE

Department of Computer Science and Engineering


Islamic University of Technology
June 18, 2026

Page 1
CSE 4635 — Web Architecture Lab 05 — Section B

1. Lab Objectives
By the end of this lab you will be able to:
1. Build a REST API using Spring Boot with the three-layer architecture (Controller →
Service → Repository).
2. Implement standard CRUD operations exposed as HTTP endpoints.
3. Use appropriate HTTP methods (GET, POST, PUT, DELETE) and status codes (200,
201, 204, 404).
4. Test API endpoints using Swagger UI or Postman.
5. Extend an existing API with filtering, search, and validation endpoints.

2. Prerequisites
Before starting this lab, make sure you are comfortable with:

Concept Where Covered


Java classes, interfaces, generics Prerequisite — OOP courses
HTTP methods and status codes Lectures 01, 07–08
REST principles and URI design Lectures 07–08
Maven project structure Lab 02 (Servlet setup)
Spring Boot annotations (@RestController,
Lectures 09–11
@Service, etc.)

3. Background

Three-Layer Architecture
Spring Boot applications are organised into three layers, each with a single responsibility:

Layer Spring Annotation Responsibility

Page 2
CSE 4635 — Web Architecture Lab 05 — Section B

Receives HTTP requests, returns HTTP


Controller @RestController
responses

Service @Service Business logic, validation, orchestration


Data access — reads and writes to the
Repository @Repository
store

The request flow is strictly top-down: a browser or Postman sends an HTTP request to the
Controller, which calls the Service, which calls the Repository. The response travels back the
same path.

Info
In this lab the Repository uses a plain ArrayList instead of a database. The layered
architecture remains identical — in a real project you would swap the ArrayList for JPA with
almost no changes to the Controller or Service.

The Book Catalog Domain


Section B’s starter project manages a catalog of books. Each Book has:
• id (Long) — auto-assigned by the repository
• title (String) — the name of the book
• author (String) — who wrote it
• isbn (String) — the International Standard Book Number
• genre (String) — e.g. FICTION, SCIENCE, HISTORY, TECHNOLOGY
• available (boolean) — true if in stock, false if checked out

Two sample books (“Clean Code” and “Dune”) are pre-loaded every time the application starts.

4. Provided Source Code


The starter project ([Link]) contains the files listed below. Do not embed them here
— open them in IntelliJ and read the comments.

File Role
Spring Boot entry point — starts the embedded Tomcat
[Link]
server
[Link] Model — the domain object with getters and setters
[Link] Repository layer — in-memory ArrayList store with
findAll, findById, save, and TODO stubs for

Page 3
CSE 4635 — Web Architecture Lab 05 — Section B

update/delete
Service layer — wraps repository calls; has TODO stubs
[Link]
for updateBook/deleteBook
Controller layer — three working endpoints (GET all,
[Link]
GET by id, POST) and TODO stubs for PUT/DELETE
Maven config — Spring Web, Validation, and springdoc
[Link]
(Swagger)
[Link] Server port and Swagger UI path

5. Getting Started
1. Unzip [Link] to a convenient location.
2. Open IntelliJ IDEA → File → Open → select the bookcatalog-api folder.
3. Wait for Maven to finish downloading dependencies (watch the progress bar at the
bottom).
4. Open [Link] and click the green Run button.
5. You should see Tomcat started on port 8080 in the console.

Warning
If IntelliJ cannot resolve Spring imports, right-click [Link] → Maven → Reload Project. If
the spring-boot-maven-plugin shows an error, verify that <version>3.3.5</version> is present
in the plugin declaration.

6. Testing Your API

Option A — Swagger UI (recommended)


Open your browser and navigate to:
[Link]
Swagger lists every endpoint with a “Try it out” button. You can send requests and inspect
responses without leaving the browser.

Option B — Postman
If you prefer Postman, create requests manually. For POST/PUT, set the body to raw JSON and
the Content-Type header to application/json. Example JSON for creating a book:

Page 4
CSE 4635 — Web Architecture Lab 05 — Section B

"title": "The Pragmatic Programmer",


"author": "David Thomas & Andrew Hunt",
"isbn": "978-0135957059",
"genre": "TECHNOLOGY",
"available": true
}

7. Lab Procedure

Part A — Explore and Run


Make sure the application is running, then test each of the three implemented endpoints:

Method URL Expected Status What It Does


Returns all books (2
GET /api/books 200 OK
seeded)
Returns the book with id
GET /api/books/1 200 OK
=1
GET /api/books/99 404 Not Found No book with id 99
Creates a new book
POST /api/books 201 Created
(send JSON body)

After testing, trace a single GET /api/books/1 request through the code:
1. Which method in [Link] handles this request?
2. Which method does the controller call in [Link]?
3. Which method does the service call in [Link]?
4. How does the repository search for a book by id?

Tip
Write a one-sentence answer for each. This trace proves you understand the three-layer
flow, and will make Parts B and C much easier.

Part B — Guided Extension (complete the TODOs)


The starter code has clearly marked TODO comments in all three layers. Complete them:

Task B1 — Update a Book (PUT /api/books/{id})


1. In [Link]: write an update(Long id, Book book) method. Find the book
by id, update its fields, and return it (or return null).

Page 5
CSE 4635 — Web Architecture Lab 05 — Section B

2. In [Link]: write updateBook(Long id, Book book) that delegates to the


repository.
3. In [Link]: add a @PutMapping("/{id}") endpoint. Return 200 OK with the
updated book, or 404 Not Found.

Task B2 — Delete a Book (DELETE /api/books/{id})


1. In [Link]: write a delete(Long id) method using removeIf(). Return true
if removed.
2. In [Link]: write deleteBook(Long id) that delegates to the repository.
3. In [Link]: add a @DeleteMapping("/{id}") endpoint. Return 204 No
Content on success, or 404 Not Found.

Test both endpoints in Swagger or Postman. Verify:


• PUT with a valid id returns 200 and the updated JSON
• PUT with an invalid id returns 404
• DELETE with a valid id returns 204 (no body)
• After deleting, GET /api/books no longer lists that book

Part C — Independent Tasks


Build these endpoints from scratch. No starter code is provided — you decide where the logic
goes across the three layers.

Task C1 — Filter by Genre


Add a query parameter to GET /api/books so that GET /api/books?genre=FICTION returns only
fiction books. If no genre parameter is provided, return all books as before.

Tip
Use @RequestParam(required = false) String genre in the controller method signature. If
genre is null, return all; otherwise filter the list.

Task C2 — Search by Keyword


Create GET /api/books/search?keyword=code that returns books whose title or author contains
the keyword (case-insensitive).

Task C3 — Validation

Page 6
CSE 4635 — Web Architecture Lab 05 — Section B

Add validation to the POST and PUT endpoints. Title and author must not be blank, and ISBN
must not be empty. Return 400 Bad Request with an error message if validation fails.

Tip
You can use Spring’s @Valid and @NotBlank annotations from the validation starter, or do
manual checks in the service layer.

Task C4 — Statistics Endpoint


Create GET /api/books/stats that returns a JSON object with: total book count, count per genre,
and the number of available vs. unavailable books.

8. Conceptual Questions
Answer each question in 2–4 sentences.

1. Q1: What is the difference between @RestController and @Controller? What happens if
you swap them?
2. Q2: Could the Controller call the Repository directly, skipping the Service layer? What
problems would that cause as the application grows?
3. Q3: Why does the POST endpoint return status 201 Created instead of 200 OK? When
is 204 No Content appropriate?
4. Q4: Explain the difference between @PathVariable and @RequestParam. Give an example
URL for each.
5. Q5: All data is lost when the server restarts. How would you make the data persistent?
Name the Spring module you would use.

9. Submission Guidelines
Submit a single zip file named Lab05_SpringBoot_<YourStudentID>.zip containing:
1. Your completed project folder (the entire bookcatalog-api directory).
2. A text file [Link] with your responses to Q1–Q5.

Warning
Do not include the target/ folder or any IDE-specific files (.idea/, *.iml). In IntelliJ, right-click
the project → Open In → Explorer, then zip the folder manually.

Page 7
CSE 4635 — Web Architecture Lab 05 — Section B

10. Assignment (Take-Home)


Extend your Book Catalog API with the following features. This is an individual assignment due
one week after the lab.

1. Add a new Author resource (id, name, nationality, birthYear) with its own Controller,
Service, and Repository. Implement full CRUD for authors.
2. Link books to authors: each Book should have an authorId field. Add GET
/api/authors/{id}/books to retrieve all books by a specific author.

3. Add sorting and pagination to GET /api/books (e.g. ?sortBy=title and ?page=&size=).
4. Add consistent error responses: a global exception handler (@RestControllerAdvice)
returning a JSON error body with a message and status for not-found and validation
errors.
5. Document every endpoint so it is clearly described in Swagger UI.

Deliverables: the zipped project (Lab05_Assignment_<YourStudentID>.zip), plus a one-


page [Link] describing your endpoints, your design decisions, and screenshots of two
endpoints tested in Swagger or Postman.

Page 8

You might also like