Skip to content

juliocesarscheidt/java-spring-rest-api-project

Repository files navigation

Java Spring REST API Project

Build PR Build Push

This project is a tiny API made with Java - Spring Boot, with Hibernate for JPA, Maven for package management, running everything inside docker containers.

There is the actuator component on Spring, to provide healthcheck and a prometheus client, also the prometheus server is running on port 9090 to scrape the actuator's data.

The CI is made with Github Actions.

It implements HATEOAS using spring hateoas.

There is a swagger documentation auto generated by springfox-swagger, accessible on:

UI http://localhost:8000/swagger-ui/index.html

JSON http://localhost:8000/v2/api-docs

Up and Running

docker-compose up -d mysql
docker-compose logs -f --tail=50 mysql

docker-compose up -d --build api
docker-compose logs -f --tail=50 api

docker-compose up -d prometheus
docker-compose logs -f --tail=50 prometheus

Run Migrations

# with docker-compose
docker-compose run api mvn flyway:migrate

# or with maven directly
mvn flyway:migrate

# or even with spring-boot
mvn clean package spring-boot:run

Format Java code with Standards

mvn com.spotify.fmt:fmt-maven-plugin:format
mvn com.spotify.fmt:fmt-maven-plugin:format -Dverbose=true

Usage

The API is available on port 8000 externally:

# check healthcheck
curl --silent -X GET --url "http://localhost:8000/actuator/health"

# check prometheus client (spring actuator)
curl --silent -X GET --url "http://localhost:8000/actuator/prometheus"

# readiness and liveness
curl --silent -X GET --url "http://localhost:8000/actuator/health/readiness"
curl --silent -X GET --url "http://localhost:8000/actuator/health/liveness"


# check prometheus server
AUTH="$(echo -n 'admin:admin' | base64 -w0)"
curl --silent -X GET -H "Authorization: Basic ${AUTH}" \
  "http://localhost:9090/metrics"


# get token
DATA='{"username": "julio", "password": "password1234"}'
TOKEN=$(curl --silent \
  -X POST \
  -H 'Content-type: application/json' \
  --data-raw "${DATA}" \
  --url "http://localhost:8000/v1/auth/signin" | jq -r '.token')
echo "${TOKEN}"


AUTH_HEADER="Authorization: Bearer ${TOKEN}"
echo "${AUTH_HEADER}"

# create customer
curl --silent -X POST \
  -H 'content-type: application/json' \
  -H "${AUTH_HEADER}" \
  --data '{"first_name": "CUSTOMER", "last_name": "CUSTOMER", "email": "[email protected]", "address": "ADDRESS", "gender": "Male"}' \
  --url "http://localhost:8000/v1/customer"
# {"id":1,"first_name":"CUSTOMER","last_name":"CUSTOMER","email":"[email protected]","address":"ADDRESS","gender":"Male","_links":{"self":{"href":"http://localhost:8000/v1/customer/1"}}}

# get all customers
curl --silent -X GET \
  -H 'content-type: application/json' \
  -H "${AUTH_HEADER}" \
  --url "http://localhost:8000/v1/customer?page=0&size=50"
# [{"id":1,"first_name":"CUSTOMER","last_name":"CUSTOMER","email":"[email protected]","address":"ADDRESS","gender":"Male","links":[{"rel":"self","href":"http://localhost:8000/v1/customer/1"}]}]

# get customer by ID
curl --silent -X GET \
  -H 'content-type: application/json' \
  -H "${AUTH_HEADER}" \
  --url "http://localhost:8000/v1/customer/1"
# {"id":1,"first_name":"CUSTOMER","last_name":"CUSTOMER","email":"[email protected]","address":"ADDRESS","gender":"Male","_links":{"self":{"href":"http://localhost:8000/v1/customer/1"}}}

# check on Database
docker-compose exec mysql mysql -uroot -padmin -h 127.0.0.1 -P3306 \
  -e "SELECT * FROM spring_rest_api_database.customer WHERE id = 1"
# +----+---------+-------------------+------------+--------+-----------+
# | id | address | email             | first_name | gender | last_name |
# +----+---------+-------------------+------------+--------+-----------+
# |  1 | ADDRESS | [email protected] | CUSTOMER   | Male   | CUSTOMER  |
# +----+---------+-------------------+------------+--------+-----------+

# update customer
curl --silent -X PUT \
  -H 'content-type: application/json' \
  -H "${AUTH_HEADER}" \
  --data '{"first_name": "CUSTOMER_CHANGED", "last_name": "CUSTOMER_CHANGED", "email": "[email protected]", "address": "ADDRESS", "gender": "Male"}' \
  --url "http://localhost:8000/v1/customer/1"
# {"id":1,"first_name":"CUSTOMER_CHANGED","last_name":"CUSTOMER_CHANGED","email":"[email protected]","address":"ADDRESS","gender":"Male","_links":{"self":{"href":"http://localhost:8000/v1/customer/1"}}}

# delete customer
curl --silent -X DELETE \
  -H 'content-type: application/json' \
  -H "${AUTH_HEADER}" \
  -I --url "http://localhost:8000/v1/customer/1"
# HTTP/1.1 204