Build a RESTful API service that allows users to create, read, update, and delete IOUs (I Owe You) using Java and Spring Boot.
Inside the ious package, create an IOU entity class with the following members:
final UUID id- unique identifier for the IOUString borrower- name of borrowerString lender- name of lenderBigDecimal amount- amount covered by IOUInstant dateTime- date and time of IOU being issuedpublic 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"
}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.
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.
Create an IOUController class with the following members:
IOUService iouService- in instance of the service interfaceIOUController(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.
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
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.
Open a terminal at the root of the repo directory and run the following command to install the dependencies:
./mvnw clean dependency:resolveIf you are on a Windows machine, that will be:
mvnw clean dependency:resolveTo start the API from the terminal, run the following command:
./mvnw spring-boot:runOr on Windows:
mvnw spring-boot:runYou 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"
}Remember that the id property may not be needed for all request types.