Skip to content

Repository files navigation

Sandbox

This application provides skeleton for starting new projects with a lot of boilerplate code. In this project used Clean\Onion Architecture. As an example used ItemEntity as a base Domain model, backed by Repository, Service and Rest Controller, validation, logging. Tests written for each layer - Repository, Service, Controller and Integration tests for the whole application.

Technology stack

  • Maven
  • Spring Boot 3
  • Postgres Database
  • Docker | Docker Compose
  • Flyway
  • MapStruct
  • Lombok
  • Swagger
  • JUnit | Mockito | MockMvc | Testcontainers

REST API

EndpointMethodReq. bodyStatusResp. bodyDescription
/api/v1/itemsGET200ItemDTO[]Get all the items
/api/v1/itemsPOSTItemDTO201ItemDTOAdd a new item
/api/v1/items/{id}GET200ItemDTOGet the item with the given ID.
404ErrorDTONo item with the given ID exists.
/api/v1/items/{id}/{locale}GET200LocalizedItemDTOGet the localized item with the given ID and Locale.
404ErrorDTONo item with the given ID exists.
/api/v1/items/{id}PUTItemDTO200ItemDTOUpdate the item with the given ID.
404ErrorDTONo item with the given ID exists.
/api/v1/items/{id}DELETE204Delete the item with the given ID.

Swagger UI is available.

Useful Commands

Maven CommandDescription
mvn spring-boot:runRun the application.
mvn clean installBuild the application.
mvn clean testRun tests.

Running a PostgreSQL Database

Run PostgreSQL as a Docker container

docker run -d \
--name sandbox-postgres \
-e POSTGRES_USER=user \
-e POSTGRES_PASSWORD=password \
-e POSTGRES_DB=sandbox_db \
-p 5432:5432 \
postgres:14.4

or run via Docker Compose

cd docker
docker-compose up

Container Commands

Docker CommandDescription
docker stop sandbox-postgresStop container.
docker start sandbox-postgresStart container.
docker remove sandbox-postgresRemove container.

Database Commands

Start an interactive PSQL console:

docker exec -it sandbox-postgres psql -U user -d sandbox_db
PSQL CommandDescription
\listList all databases.
\connect sandbox_dbConnect to specific database.
\dtList all tables.
\quitQuit interactive psql console.

Internationalization (i18n)

Data localization

In a case you need translated or adjusted response according to the certain location, there is an example with translated currency code by requested locale on the /api/v1/items/{id}/{locale} endpoint. It translates Entity field currencyCode with the most matching locale among those present in ResourceBundle files in the src/main/resources/resourcebundle directory.

Exception localization

For translation of REST request arguments validation messages, there is a ValidationExceptionHandler class that handles
MethodArgumentNotValidException type exceptions and translates its messages by the language header. Validation happens before the REST controllers method invocation.

Spring Actuator

The following Spring Actuator endpoints are used for monitoring application state and retrieving the corresponding data:

  • /actuator Use this endpoint to get a list of all endpoints enabled in Spring Actuator.
  • /actuator/health Use this endpoint to get the basic health information about the application.
  • /actuator/metrics Use this endpoint to get a list of available application metrics.
  • /actuator/metrics/{MetricName} Use this endpoint to receive data from the specific metric.
  • /actuator/info Use this endpoint to display the application-specific information.
  • /actuator/httpexchanges Use this endpoint to get traces of all the HTTP requests have been processed by the application.
  • /actuator/flyway Use this endpoint to get details about the Flyway database migrations.
  • /actuator/beans Use this endpoint to get all available beans created by the application.

Authentication

The application utilizes the Basic HTTP authentication. It requires the Authorization: Basic header followed by the login:password pair to be included in all requests made to the application. The login and password are specified in the credentials.properties file.

To disable the authentication for the entire application, use the app:security:enabled property of the application.yml.

Note, that the login:password pair used in the authorization header should be encoded in base64 format:

### Get all itemsGET http://localhost:8081/actuatorAuthorization: Basic dXNlcjpwYXNzd29yZA==

About

Java Sandbox for R&D

Resources

Stars

1 star

Watchers

7 watching

Forks

Releases

Packages

Contributors

Languages