Using the repository to build the Java Spring Boot Rest API application with CQRS
This is version
1.0.0 of the
Running the application locally
Clone the Java project to your local machine from here: stacks-java repository
Configure required environment variables
The application is currently configured to work with the Azure environment.
It uses an Azure CosmosDB database to store the example application data. So you should have access to an instance to use with the application. Note: For running on a local Windows environment you can use the Cosmos DB emulator (CosmosDB Emulator has a known fixed key). For further info please follow the link.
In addition, Azure ApplicationInsights is used for logging purposes. If this is unavailable, modify the application so that it doesn't fail to startup if it can't access ApplicationInsights, and simply log to the terminal instead.application-insights:instrumentation-key: xxxxxxenabled: false
There are two corresponding environment variables that need to be set to interact with these systems:AZURE_COSMOSDB_KEYAZURE_APPLICATION_INSIGHTS_INSTRUMENTATION_KEY
Set the two environment variables as additional variables within e.g. ~/.profile or /etc/profile.
Build and run the application
Note that at a minimum Java 11 should be installed.
Move to the
Build and run the application using Cosmos DB Emulator please refer to section "Determine which root certificates have been installed" in Setting Up CosmosDB Emulator
Move to the
<PROJECT-NAME>/javafolder, then go to
application.ymleither comment out the
application-insightsblock or set
logback-spring.xmlcomment out the application-insight section.
AZURE_COSMOSDB_KEYas an environment variable and set the value to be the primary key value on the emulator.
./mvnw spring-boot:run -Dspring-boot.run.jvmArguments='-Djavax.net.ssl.trustStore="<Location of the root cosmos db certificate>" -Djavax.net.ssl.trustStorePassword="changeit"'
Verify that the application has started
Browse to http://localhost:9000/v1/menu. This should return a valid JSON response.
The application configuration uses Swagger/OAS3 to represent the API endpoints. The Swagger UI can be viewed by directing your browser to http://localhost:9000/swagger/index.html.
All API endpoints are (optionally) protected using Auth0. There is an
auth.properties file within the project codebase.
If the following property within this file is set:
then clients will need to pass an
Authorization header containing the Bearer token generated from Auth0 as part of the endpoint request. If the value
is set to
false then no authorization is required.
Auth0 configuration properties
If using Auth0 for authorization, Auth0 itself will need to be configured with both an API definition and an associated Application.
There are corresponding configuration values required for the Stacks application, within the
auth.properties file, e.g.
These parameters are used to verify that the JWT supplied in the Authorization header of a request is valid.
- Automatically generated for the project. Go to Swagger Index to view.
- Swagger Json is here: Swagger Json
- Available at: health check (This can also be configured to run on another port)
Using a Docker image
<PROJECT-NAME>/java folder, build a Docker image using e.g. the command below:
This uses the
Dockerfile in this folder to generate the Docker image.
If you have an
.m2 directory in the
java/ folder, the Docker build will attempt to copy the files inside the container and use the cached versions.
Once the Docker image is created, you can then run a Docker container based on this image using e.g.
which passes in the two required environment variables from your own environment.