Absolutely! For the caching requirement, let's create a take-home assignment that involves building a caching layer into a weather forecasting API.
Project: Weather Forecasting System API
Objective: Implement a RESTful API for a Weather Forecasting System using Java, Spring Boot, and a caching mechanism. The API should allow users to request current weather forecasts for different cities and cache the results to improve performance.
Requirements:
-
Initialize a new Spring Boot project using Spring Initializr. Choose the following dependencies: Web, JPA, H2, Cache, and RestTemplate.
-
Structure your project with the following packages:
model
: Contains domain model classes (City
,Forecast
)repository
: Contains repository interfaces extendingJpaRepository
service
: Contains service classes to encapsulate business logiccontroller
: Contains REST API controllersexception
: Contains custom exception classesdto
: Contains data transfer objects (DTOs) used in the API
-
Implement the domain model:
City
class: Fields should includeid
,name
, and a list ofForecast
relationships (OneToMany)Forecast
class: Fields should includeid
,timestamp
,temperature
,humidity
,windSpeed
, and aCity
relationship (ManyToOne)
-
Implement the repositories:
CityRepository
: ExtendsJpaRepository<City, Long>
ForecastRepository
: ExtendsJpaRepository<Forecast, Long>
-
Implement the services:
CityService
: Contains methods for creating, updating, deleting, and retrieving citiesForecastService
: Contains methods for retrieving forecasts; it should use RestTemplate to call an external weather API (e.g., OpenWeatherMap, WeatherStack) and then store the results in a cache before returning them
-
Implement the REST API controllers:
CityController
: Handles HTTP requests for CRUD operations on citiesForecastController
: Handles HTTP requests for retrieving forecasts
-
Implement DTOs for the API:
CityDTO
: Contains fields forid
,name
ForecastDTO
: Contains fields forid
,timestamp
,temperature
,humidity
,windSpeed
, and aCityDTO
-
Implement exception handling:
- Create custom exceptions (
ResourceNotFoundException
,BadRequestException
) - Implement a global exception handler with
@ControllerAdvice
to handle exceptions and return appropriate HTTP response status codes
- Create custom exceptions (
-
Configure an H2 in-memory database for the application.
-
Configure Spring's caching mechanism to cache forecasts for each city. You can use Spring's
@Cacheable
annotation to do this. -
Write unit tests for the service classes using JUnit and Mockito.
-
(Optional) Implement API documentation using Swagger or OpenAPI.
Deliverables:
- A fully functional Spring Boot project, including source code and tests
- A README file with instructions on how to build and run the project, as well as an explanation of any design decisions you made
When you submit your project, please include all necessary files in a single archive (e.g., ZIP or TAR) and provide clear instructions for building and running your application. Good luck!