Week 4 - Rest APIs

Java Annotations

Introduction to REST

Spring Boot Setup

Writing Endpoints

Message Formats

Input Validation

Practice

Assignment

Back end Track

Content

<aside> ๐Ÿšง

This page is currently under construction. Please check back later.

</aside>

Letโ€™s get practical

<aside> ๐Ÿ’ญ

Practice exercises are optional and do not need to be submitted

</aside>

Exercise 1 - REST Design: Notes API

In this exercise, you will design a small REST API before writing any code. The API manages personal notes.

A note has these fields:

Tasks

For each action in the table below:

The first row is pre-filled as an example.

Action Method and URI Request body&Content-Type Response body&Content-Type
Get all notes GET /api/v1/notes None JSON array of all notes. application/json
Get all notes. Filtered by tag
Get only pinned notes
Get a note by ID
Create a new note
Replace an existing note
Delete a note

Exercise 2 ย - Build a Notes API

In this exercise, you will build the API you designed in Exercise 1. You will practice Spring Boot controllers, composed mapping annotations,ย @PathVariable,ย @RequestParam,ย @RequestBody, andย ResponseEntity.

Package Structure

๐Ÿ“‚ note/
โ”œโ”€โ”€ ๐Ÿ“„ NoteController.java
โ””โ”€โ”€ ๐Ÿ“‚ models/
    โ””โ”€โ”€ ๐Ÿ“„ Note.java

Acceptance criteria

Exercise 3

In this exercise youโ€™ll expand the controller in Exercise 2 to be working with DTOs and keep your Note model internal. This will make the API boundary clearer.

Package Structure

๐Ÿ“‚ note/
โ”œโ”€โ”€ ๐Ÿ“„ NoteController.java
โ”œโ”€โ”€ ๐Ÿ“‚ย dto/
โ”‚   โ”œโ”€โ”€ ๐Ÿ“‚ย request/
โ”‚   โ”‚   โ”œโ”€โ”€ ๐Ÿ“„ CreateNoteRequest.java
โ”‚   โ”‚   โ””โ”€โ”€ ๐Ÿ“„ UpdateNoteRequest.java
โ”‚   โ””โ”€โ”€ ๐Ÿ“‚ย response/
โ”‚       โ””โ”€โ”€ ๐Ÿ“„ NoteResponse.java
โ””โ”€โ”€ ๐Ÿ“‚ models/
    โ””โ”€โ”€ ๐Ÿ“„ Note.java

Tasks

Exercise 4

Now make your Notes API reject invalid input. You will practice Jakarta Bean Validation,ย @Valid, and a global exception handler.

Validation Dependency

If you havenโ€™t added the validation dependency, you can add the below to pom.xml and reload Maven:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-validation</artifactId>
</dependency>

Acceptance Criteria


The HackYourFuture curriculum is licensed underย CC BY-NC-SA 4.0 *https://hackyourfuture.net/*

CC BY-NC-SA 4.0 Icons

Built with โค๏ธ by the HackYourFuture community ยท Thank you, contributors

Found a mistake or have a suggestion? Let us know in the feedback form.