GithubHelp home page GithubHelp logo

kikecancode / java-rest-api-exercise Goto Github PK

View Code? Open in Web Editor NEW

This project forked from cbfacademy/java-rest-api-exercise

0.0 0.0 0.0 101 KB

Define a practice exercise for students to bring together learning around APIs (Clients & Servers, Web Apps and HTTP, APIs, and Spring Boot)

License: MIT License

Java 100.00%

java-rest-api-exercise's Introduction

Java RESTful API Exercise: IOU Service

Description

Build a RESTful API service that allows users to create, read, update, and delete IOUs (I Owe You) using Java and Spring Boot.

Implement the Data Model

Inside the ious package, create an IOU entity class with the following members:

  • final UUID id - unique identifier for the IOU
  • String borrower - name of borrower
  • String lender - name of lender
  • BigDecimal amount - amount covered by IOU
  • Instant dateTime - date and time of IOU being issued
  • public IOU(String borrower, String lender, BigDecimal amount, Instant datetime) - constructor that should generate a new UUID, use the documentation to learn how to generate a random value.

Set the appropriate accessibility modifiers for all members and add appropriate property getters and setters.

A representative HTTP response will look something like this:

{
    "id": "5a6b7692-2322-482e-80cf-a59eedb9164f",
    "borrower": "Bob",
    "lender": "Alice",
    "amount": 1877.51,
    "date": "2023-04-23T18:25:43.511Z"
}

Implement the Repository

A repository interface, IOURepository has been provided for you. Create a class that implements this interface using a List instance as the backing store and name it something sensible, e.g. ListIOURepository, with the following members:

  • final List<IOU> ious = new ArrayList<>() - list of IOUs stored in the API

Set the appropriate accessibility modifiers for all members and annotate the class as a Repository. Pay attention to the exceptions defined on the interfaces and ensure your concrete class throws as appropriate.

Implement the Service

A service interface, IOUService has been provided for you. Create a class that implements this interface using a List instance as the backing store and name it something sensible, e.g. ListIOUService, with the following members:

  • final List<IOU> ious = new ArrayList<>() -
  • public ListIOUService (IOURepository iouRepository)

Set the appropriate accessibility modifiers for all members and annotate the class as a Service. Ensure your code appropriately handles any exceptions thrown by the repository class.

Implement the Controller

Create an IOUController class with the following members:

  • IOUService iouService - in instance of the service interface
  • IOUController(IOUService iouService) - constructor that accepts an instance of the service interface

Add additional methods that defines endpoints for the following operations:

Method URL Description
GET /api/ious Get all IOUs
GET /api/ious/{id} Get an IOU by id
POST /api/ious Add an IOU
PUT /api/ious/{id} Replace an IOU by Id
DELETE /api/ious/{id} Delete an IOU by id

Set the appropriate accessibility modifiers for all members and annotate the class as a RestController.

Learning Objectives:

By the end of this exercise, you should be able to:

  • Set up a Spring Boot project using a development environment
  • Create a simple RESTful API for IOU tracking using controllers, services and models.
  • Implement CRUD operations (Create, Read, Update, Delete) for IOUs
  • Implement graceful exception handling

Getting Started

Clone the Repository

Clone this repository or or open in CodeSpaces.

git clone [REPO_URL]
cd [REPO_NAME]

Replace [REPO_URL] with the link to your GitHub repository and [REPO_NAME] with the repository's name.

Install Dependencies

Open a terminal at the root of the repo directory and run the following command to install the dependencies:

./mvnw clean dependency:resolve

If you are on a Windows machine, that will be:

mvnw clean dependency:resolve

Running the Application

To start the API from the terminal, run the following command:

./mvnw spring-boot:run

Or on Windows:

mvnw spring-boot:run

Testing the Application

You can test your endpoints using Postman or your preferred REST client.

For POST and PUT requests, you'll need to provide a request body in JSON format, e.g.:

{
  "id": "d1415cfc-dbd9-4474-94fc-52e194e384fa",
  "borrower": "John Doe",
  "lender": "Alice Smith",
  "amount": 100.0,
  "dateTime": "2023-11-02T14:30:00Z"
}

๐Ÿ’ก Note

Remember that the id property may not be needed for all request types.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.