Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/elidaniel92/jetty-12-rest-api-sample
Simple REST API with Jetty 12
https://github.com/elidaniel92/jetty-12-rest-api-sample
crud crud-api docker docker-compose jackson java jdbc jetty jetty-server log4j2 maven mysql rest rest-api restful-api
Last synced: 7 days ago
JSON representation
Simple REST API with Jetty 12
- Host: GitHub
- URL: https://github.com/elidaniel92/jetty-12-rest-api-sample
- Owner: elidaniel92
- License: mit
- Created: 2024-09-10T02:50:40.000Z (about 2 months ago)
- Default Branch: main
- Last Pushed: 2024-09-13T04:29:49.000Z (about 2 months ago)
- Last Synced: 2024-10-10T16:22:04.984Z (28 days ago)
- Topics: crud, crud-api, docker, docker-compose, jackson, java, jdbc, jetty, jetty-server, log4j2, maven, mysql, rest, rest-api, restful-api
- Language: Java
- Homepage:
- Size: 29.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# jetty-12-rest-api-sample
This project serves as a boilerplate for building pure Jetty 12 (Servlet) using the following technologies and tools:
- [**Java 17**](https://www.oracle.com/br/java/technologies/downloads/): The latest long-term support version of Java, ensuring modern language features and improvements.
- [**Jetty 12**](https://jetty.org/): Eclipse Jetty provides a highly scalable and memory-efficient web server and servlet container, supporting many protocols such as HTTP/3,2,1 and WebSocket.
- [**Jackson**](https://github.com/FasterXML/jackson): The Java JSON library.
- [**Maven**](https://maven.apache.org/): Used for project management and dependency management.
- [**Docker Compose**](https://docs.docker.com/compose/): Simplifies local development by allowing you to run the entire application stack (including MySQL) within Docker containers.
- [**Dirk**](https://github.com/hjohn/Dirk): A lightweight dependency injection library for managing object creation and dependencies.
- [**SLF4J with Log4j2**](https://logging.apache.org/log4j/2.x/): Provides robust and flexible logging through the SLF4J API, using Log4j2 as the logging backend.
- [**JDBC**](https://docs.oracle.com/javase/tutorial/jdbc/): Utilized for establishing a connection to relational databases and performing SQL operations.
- [**MySQL Database**](https://www.mysql.com/): The database for storing and managing application data.This setup is ideal for jump-starting a Jetty 12 (Servlet) project with dependency injection, logging, and database connectivity, all contained in a local development environment via Docker Compose.
# Getting started
## Installation
[Install Docker](https://www.docker.com/)
[Install JDK 17](https://www.oracle.com/br/java/technologies/downloads/) (optional)
[Install Maven](https://maven.apache.org/) (optional)
> **Tip:** you can run Maven commands without having Maven installed.
```
./mvnw foo
```Clone the repository
```
git clone https://github.com/elidaniel92/jetty-12-rest-api-sample.git
```Switch to the repo folder
```
cd jetty-12-rest-api-sample
```
### Install dependencies (optional)```
mvn clean install
```## Running the App with Docker Compose
Build the App and MySQL Container Image
```
docker-compose build
```Started the container
```
docker-compose up
```## Running the Java Application locally and MySQL Server in the container
### Build the MySQL Container Image
Since you cannot load an environment file using the `build command` command, go to the MySQL Dockerfile and uncomment the environment variables section.
```
docker build -t db-img -f docker/Dockerfile.mysql .
```
### Start the MySQL Server Container
```
docker run --name db-container -ip 3306:3306 db-img
```
After the message below, the server is ready for connections. You can close the terminal:
```
2024-09-07T22:48:15.362678Z 0 [System] [MY-010931] [Server] /usr/sbin/mysqld: ready for connections. Version: '8.4.2' socket: '/var/run/mysqld/mysqld.sock' port: 3306 MySQL Community Server - GPL.
```
### Setting the Java Application Environment VariableLoad the [local environment variables file (env/local.env)](env/local.env) in your terminal session.
Windows - To load the env/local.env file in a PowerShell terminal session, use the command below:
```
Get-Content env/local.env | ForEach-Object { if ($_ -match '^(.*?)=(.*)$') { [System.Environment]::SetEnvironmentVariable($matches[1], $matches[2]) } }
```
Linux - To load the env/local.env file in a Bash terminal session, use the command below:
```
export $(grep -v '^#' env/local.env | xargs)
```### Running the app
#### With .jar
Generete .jar
```
mvn clean package
```
Run .jar
```
java -jar target/jetty-12-rest-api-sample-1.0-SNAPSHOT.jar
```
#### With Maven
```
mvn clean compile exec:java
```## Test:
CRUD (create, read, update and delete).
**POST**
```
curl --location --request POST 'http://localhost:8080/api/products' \
--header 'Content-Type: application/json' \
--data '{
"name": "Keyboard2",
"price": 50,
"quantityInStock": 150
}'
```
**GET**
```
curl --location --request GET 'http://localhost:8080/api/products/2'
```
**PUT**
```
curl --location --request PUT 'http://localhost:8080/api/products/2' \
--header 'Content-Type: application/json' \
--data '{
"name": "Keyboard2",
"price": 50,
"quantityInStock": 150
}'
```
**DELETE**
```
curl --location --request DELETE 'http://localhost:8080/api/products/2'
```
**GET LIST**
```
curl --location --request GET 'http://localhost:8080/api/products'
```## Delete All Images and Containers
```
docker rm -f db-container
docker rmi db-img
docker rm -f store-system-app-1
docker rm -f store-system-db-1
docker rmi -f store-system-app
docker rmi -f store-system-db
```# License
This project is licensed under the MIT License - see the [LICENSE](./LICENSE) file for details.