This project is a demo application that demonstrates the integration of Spring Boot with Elasticsearch. It fetches weather data from an external API (MetaWeather), stores it in Elasticsearch, and exposes a simple REST API to retrieve weather information for a given location.
Before you begin, ensure you have the following installed:
- Java Development Kit (JDK) 11 or higher
- Apache Maven
- Docker (for running Elasticsearch)
- Clone the repository:
git clone https://github.com/your-username/springboot-elasticsearch.git cd springboot-elasticsearch
- Start Elasticsearch using Docker Compose:
In the root directory of the project, run:
This will start an Elasticsearch container in the background.
docker-compose up -d
- Build the application:
./mvnw clean install
- Run the application:
The application will start on port 8080 by default.
java -jar target/springboot-elasticsearch-0.0.1-SNAPSHOT.jar
The application exposes the following API endpoint:
- GET
/weather
- Description: Retrieves weather information for a specific location using its "Where On Earth ID" (WOEID).
- Request Parameters:
locationId
(string, required): The WOEID of the location. Defaults to44418
(London) if not provided.
- Example Request (using curl):
This will fetch the weather for San Francisco (WOEID: 2487956). To get weather for the default location (London):
curl "http://localhost:8080/weather?locationId=2487956"
curl "http://localhost:8080/weather"
src/main/java
: Contains the main source code for the application.client
: Components for interacting with external APIs (e.g.,WeatherApiClient
).config
: Configuration classes for Spring Boot and Elasticsearch.controller
: Spring MVC controllers that handle incoming HTTP requests (e.g.,WeatherController
).model
: Data model classes (e.g.,Location
).repository
: Spring Data Elasticsearch repositories for data access (e.g.,WeatherRepository
).service
: Business logic components (e.g.,WeatherService
).
src/main/resources
: Contains application resources.application.yml
: Configuration file for Spring Boot.sample.json
: An example of the JSON structure used by the weather API.
pom.xml
: The Maven Project Object Model file, defining project dependencies and build configuration.docker-compose.yml
: Docker Compose file for setting up the Elasticsearch service.README.md
: This file.
Contributions are welcome! If you find any issues or have suggestions for improvements, please open an issue or submit a pull request.
This project is open source and available under the MIT License.