Skip to content

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.

Notifications You must be signed in to change notification settings

NelushGayashan/WeatherApp

Repository files navigation

WeatherApp

Overview

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.

Features

  • 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 @Async and CompletableFuture.
  • Includes unit tests for service and controller layers using JUnit and Mockito.

Technologies Used

  • 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)

Architecture Overview

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 @Async and CompletableFuture to perform non-blocking API calls.

Requirements

Input

A city name provided as a query parameter to the endpoint:

GET /weather?city=London

Output

A JSON response summarizing the weather for the given city:

{
  "city": "London",
  "averageTemperature": 15.5,
  "hottestDay": "2024-11-20",
  "coldestDay": "2024-11-18"
}

Technologies Used

  • 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)

Setup & Installation

Prerequisites

Ensure you have the following installed:

  • Java 17+
  • Maven 3+
  • An OpenWeatherMap API key

Steps to Run

  1. Clone the repository:
git clone https://github.com/NelushGayashan/WeatherApp.git
cd WeatherApp
  1. Configure the OpenWeatherMap API key: To use the weather API, you need to set your OpenWeatherMap API key as an environment variable.

Steps:

  1. Obtain your API key from OpenWeatherMap.

  2. 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"
  3. The application will automatically use this environment variable to access the API key. Make sure to replace your_openweathermap_api_key with the actual key you received.

  4. Build and run the application:

mvn clean install
mvn spring-boot:run
  1. Access the API:
http://localhost:8080/weather?city=London

Caching Strategy

  • Uses Spring Cache with Caffeine.
  • Cache timeout: 30 minutes.
  • Cache key: City name.

Error Handling

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.

Running Tests

Run unit tests using:

mvn test

Sample API Calls & Responses

Successful Response

Request:

GET /weather?city=London

Response

Successful Response

Response:

{
  "city": "London",
  "averageTemperature": 15.5,
  "hottestDay": "2024-11-20",
  "coldestDay": "2024-11-18"
}

Error Response (Invalid City)

Request:

GET /weather?city=InvalidCity

Response:

{
  "error": "City not found: InvalidCity"
}

License

This project is licensed under the MIT License.

About

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.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages