- Port Used: 8080
- Credentials for API Authentication:
- Username: user
- Password: password
- The Flow diagram is provided for better understanding of the project.
Flow-Diagram.png
This project is a weather prediction application developed using Java, Spring Boot, and Gradle. It provides weather forecasts for different cities. The application fetches data from an external weather API and processes it to provide detailed weather forecasts.
- Java (Spring Boot)
- Gradle
- JUnit
- Docker
- Jenkins
- Swagger
- Java 21
- Gradle 3.2.2
The assignment requires development, testing, and deployment of a microservice to display the high and low temperatures for the next three days of a specified city. If rain is predicted in the next three days or if the temperature exceeds 40 degrees Celsius, the output for that day should include 'Carry umbrella' or 'Use sunscreen lotion' respectively with some additional requirements such as in case of high winds Wind 10mph mentionioning “It’s too windy, watch out!” and in case of Thunderstorms, mention “Don’t step out! A Storm is brewing!”.
- Open your preferred IDE and import the project. The project is Java-based, so it requires JDK 21 and Gradle 3.2.2 (or later) to run. In the terminal, navigate to the project directory and run the following commands:
cd weather
gradle build
java -jar build/libs/weather-0.0.1-SNAPSHOT.jar
- Another way to run this API is to use Docker. You should have Docker Desktop installed on your machine. Build the Docker image and Run the Docker container using the following commands: Run the following commands in the terminal:
docker build -t weather .
docker run -it -p 8080:8080 weather
The API has one endpoint:
- /v1/weather/getWeatherByCity?city={city} - This endpoint fetches the weather forecast of 3 days of weather forecast including today's forecast for the given city.
- /v1/weather/getAllWeatherByCity?city={city} - This endpoint fetches the detailed weather forecast of 5 days of weather forecast including today's forecast for the given city.
- The API can be tested using Postman / React UI.
1.1 Get Weather of City for 3 days
curl --location 'http://localhost:8080/v1/weather/getWeatherByCity?city=london&isChecked=false' \
--header 'Authorization: Basic dXNlcjpwYXNzd29yZA=='
1.2 Get All Detail Weather Information of City for 5 days
curl --location 'http://localhost:8080/v1/weather/getAllWeatherByCity?city=london' \
--header 'Authorization: Basic dXNlcjpwYXNzd29yZA=='
- Swagger is also included in the API .
- Open your browser and navigate to the following URL:
http://localhost:8080/swagger-ui/index.html
- Jenkins Script is also included in the project to automate the build and deployment process in the below file.
Jenkins.config
- Docker file is also included in the project in the below file.
Dockerfile
- Authentication and Authorization is used to secure the API using Spring Security.
- The credentials are passed in CI / CD pipeline in the Jenkins file.
- The API Key is also passed in CI / CD pipeline the Jenkins file.
- Global Exception Handling is used to handle the exceptions in the API with '@ControllerAdvice' and '@ExceptionHandler' annotations.
This project adheres to SOLID principles:
-
Single Responsibility Principle (SRP): Each class has one responsibility. For instance,
WeatherClient
communicates with the weather API,WeatherDetailsService
processes the weather data, andWeatherController
handles HTTP requests and responses. -
Open-Closed Principle (OCP): Classes are open for extension but closed for modification. This is achieved by defining interfaces and implementing them in specific classes.
-
Interface Segregation Principle (ISP): Each class should only need to know about the methods it actually uses. If the application uses interfaces, they should be small and specific rather than large and general.
-
Dependency Inversion Principle (DIP): High-level modules should not depend on low-level modules. Both should depend on abstractions. This is achieved by using dependency injection to inject dependencies, such as services or repositories, into classes. This makes the code more flexible and easier to modify.
- The API follows HATEOAS principles. It includes links to related resources in the response. For example, the response for the
/v1/weather/getWeatherByCity?city={city}
endpoint includes links to the/v1/weather/getAllWeatherByCity?city={city}
endpoint.
This project utilizes several design patterns:
- Factory Pattern: Used for creating weather data objects from the external weather API.
- Singleton Pattern: Ensures a single instance of
WeatherClient
for API communication. - Strategy Pattern: Handles different weather data processing strategies.
- Dependency Injection: A Spring framework feature, allowing for inversion of control.
- Model-View-Controller (MVC):
WeatherController
(Controller),WeatherClient
andWeatherDetailsService
(Model) handle the business logic, and the views are the responses sent to the client.