Week 4 - Rest APIs

Java Annotations

Introduction to REST

Spring Boot Setup

Writing Endpoints

Message Formats

Input Validation

Practice

Assignment

Back end Track

Content

Let’s get practical

<aside> 💭

Practice exercises are optional and do not need to be submitted

</aside>

For Exercises 1 and 2, use a small Spring Boot project with Spring Web, Lombok, and Validation if needed. You may create a fresh project for each exercise, or keep each exercise in its own feature package inside one practice project.

Exercise 1 - Design a REST API

Choose a simple resource that is either:

Create a markdown file named API_DESIGN.md and design the API you would build for that resource.

Requirements:

Exercise 2 - Build a Service+Controller Combo

Choose one pair of resources:

For one resource, start by writing the controller first:

For the other resource, start by writing the service first:

API Endpoints

Limit each resource to three endpoints: GET all items, POST add a new item, and DELETE delete an item.

Reflection Questions

After implementing both approaches, answer the following:

  1. Which approach let you get a working HTTP endpoint faster? Why?
  2. What challenges did you come across in either approach?
  3. Which approach felt more maintainable, and why?

Exercise 3.1 - Find and Handle Exceptions

Go back to the Categories API you built in Writing Endpoints. Remove any exception handlers except the handler for MethodArgumentNotValidException that we implemented in Input Validation.

Then, from Postman, try sending bad inputs to find more unhandled exceptions. Hint: There are a few.

How many can you find? Can you handle them? What would be an appropriate message?

<aside> 💡

Run your application without debug mode. This makes it easier to see thrown exceptions either in the Postman response or in the application console, near Spring Boot's startup message.

</aside>

Exercise 3.2 - More Validations

While working through the previous exercise, did you find validation scenarios that were missing?

Think in terms of:

List them and implement the missing validations.


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.