WeatherApp is a Spring Boot application that fetches, processes, and analyzes weather data asynchronously. It retrieves weather forecasts from an external API (OpenWeatherMap) and provides a summary, including the average temperature, hottest day, and coldest day for the past 7 days.
- Fetches weather data using WebClient.
- Processes and analyzes weather data.
- Computes the average temperature, hottest day, and coldest day.
- Caches results for 30 minutes using Caffeine Cache.
- Handles API failures and invalid city names gracefully.
- Implements asynchronous execution with
@AsyncandCompletableFuture. - Includes unit tests for service and controller layers using JUnit and Mockito.
- Java 17
- Spring Boot 3+
- Spring Web
- Spring Cache (Caffeine)
- Spring WebFlux (for WebClient)
- Spring Boot Starter Test
- Lombok (for reducing boilerplate code)
- Mockito & JUnit (for unit testing)
The application follows a layered architecture:
- Controller Layer: Handles incoming HTTP requests.
- Service Layer: Fetches, processes, and caches weather data.
- Exception Handling: Provides robust error handling for API failures and invalid inputs.
- Caching Layer: Uses Caffeine Cache to store results for 30 minutes.
- Asynchronous Execution: Uses
@AsyncandCompletableFutureto perform non-blocking API calls.
A city name provided as a query parameter to the endpoint:
GET /weather?city=LondonA JSON response summarizing the weather for the given city:
{
"city": "London",
"averageTemperature": 15.5,
"hottestDay": "2024-11-20",
"coldestDay": "2024-11-18"
}- Java 17
- Spring Boot 3+
- Spring Web
- Spring Cache (Caffeine)
- Spring WebFlux (for WebClient)
- Spring Boot Starter Test
- Lombok (for reducing boilerplate code)
- Mockito & JUnit (for unit testing)
Ensure you have the following installed:
- Java 17+
- Maven 3+
- An OpenWeatherMap API key
- Clone the repository:
git clone https://github.com/NelushGayashan/WeatherApp.git
cd WeatherApp- Configure the OpenWeatherMap API key: To use the weather API, you need to set your OpenWeatherMap API key as an environment variable.
-
Obtain your API key from OpenWeatherMap.
-
Set the API key as an environment variable on your system:
-
On Linux/macOS:
export WEATHER_API_KEY=your_openweathermap_api_key -
On Windows (Command Prompt):
set WEATHER_API_KEY=your_openweathermap_api_key
-
On Windows (PowerShell):
$env:WEATHER_API_KEY="your_openweathermap_api_key"
-
-
The application will automatically use this environment variable to access the API key. Make sure to replace
your_openweathermap_api_keywith the actual key you received. -
Build and run the application:
mvn clean install
mvn spring-boot:run- Access the API:
http://localhost:8080/weather?city=London- Uses Spring Cache with Caffeine.
- Cache timeout: 30 minutes.
- Cache key: City name.
| Error Type | HTTP Status | Description |
|---|---|---|
| Invalid City Name | 404 | The specified city was not found. |
| API Key Invalid/Missing | 401 | The provided API key is incorrect. |
| External API Failure | 503 | OpenWeatherMap API is unavailable. |
| Generic Error | 500 | An unexpected error occurred. |
Run unit tests using:
mvn testRequest:
GET /weather?city=LondonResponse:
{
"city": "London",
"averageTemperature": 15.5,
"hottestDay": "2024-11-20",
"coldestDay": "2024-11-18"
}Request:
GET /weather?city=InvalidCityResponse:
{
"error": "City not found: InvalidCity"
}This project is licensed under the MIT License.