There are 3 parts in this tutorial: the first one is related with Spring Boot REST API, the second one is related with adding DB support with AWS DynamoDB and the third one is related with Docker. Without further ado, let’s start with the first part.

1) Building a Java Spring Boot REST API

For this part, please refer to this tutorial: https://dzone.com/articles/spring-boot-restful-web-service-complete-example (link here). I have summarized some important items below:

  • The core requirements are the Java development Kit (JDK), Eclipse and the Spring Tool Suite 4 (STS).
    • Once you have all requirements in place, you just need to create a project using the template «Spring Starter Project «.
    • Spring Boot is a light weight Spring framework, more info here.
    • You could also build apps without Spring. Another option could be using maven archetypes as described in this link.
      • If the archetype is missing, you need to install it as described here.
  • Important Implementation Details:
    • Add the controller class annotation @RestController and @CrossOrigin(origins = «http://localhost:4200») to your controller.
    • Add the function level annotations @GetMapping(«/car») and @RequestMapping(method=RequestMethod.POST, value=»/cars»)
    • Add models with getters and setters.
    • You will need to create packages for each component, incl. your models, controllers and repository.
    • Double check if you have all dependencies in the file pom.xml. You can compare with the provided source code.
    • Add any configuration to the file application.properties
  • Once ready, run git add ., git commit -m «comment», git push -u origin master
  • To build the app, use the command mvn clean package
  • To run the app, use the command java -jar <file_name>.jar
  • You can optionally test the app using SoapUI.

2) Connecting to AWS DynamoDB

In this example we’re using AWS DynamoDB. There are two ways to interact with any DB: one is using an Object Relational Model (ORM is an important concept, this is explained here) and the second one is more manual, using the functions available in the AWS SDK (link here). The steps below are inspired in the ORM mapper (first approach):

  • First step is to add the following annotations to your model class: @DynamoDBTable,
    @DynamoDBAttribute and @DynamoDBHashKey.
  • Then create an interface extending from class CrudRepository<T, String>.
    • CrudRepository is an interface for generic CRUD operations in Spring Data Core 2.2.0 API.
    • JPA is an important concept: Spring Data API is an implementation of the Java Persistence API (JPA), similar to hibernate.
    • For .NET Developers: CrudRepository is a similar concept than .NET DbContext from the Entity Framework API.
  • Optional: Dependency Injection (DI)
    • In this specific example, you can use DI to inject the configuration, which is more specifically the AWS secret and access keys. With this purpose in mind, follow the next optional steps:
      • Create a Config class and do the following:
        • Decorate it with the annotations: @Configuration and
          @EnableDynamoDBRepositories.
        • Add the annotation @Value to the variables mapping properties in the application.properties file.
        • Add the annotation @Bean to the functions in the class. This annotation is part of the Spring Java Config API.
      • Add the attribute @Autowired to the variable Repository in your controller.
        • The annotation @Autowired is part of Spring Framework API.
        • The annotations @Autowired and @Configuration are the core of Dependency Injection in Spring (more information here). DI is an important concept, some terminology below.
          • Inversion of Control (IoC): process of moving the responsibility for creating a dependency outside the class depending on it.
          • Dependency Injection (DI): means that this is done without the object intervention, usually by a framework component
          • IoC containers: the container becomes responsible for injecting the right dependency.
          • The IoC Container will manage the types/classes/beans returned by functions annotated with @Bean (more info herehere).
          • For .NET Developers: in .NET, you could use AutoFac.
      • Add the annotation @EnableScan to the Repository class.
        • The annotation @EnableScan is part of Spring Data DynamoDB API.

3) Containerize your REST API

The first part is selecting a Java servlet container. A servlet container is an application server that implements some version of the Java Servlet Specification. Good news is that Spring Boot includes embedded Embedded Servlet Containers Tomcat, Undertow and Jetty. IN our example we’re using the default embedded Tomcat (more details here).

The next task when containerizing (i.e.: moving an app to a docker container) any app is the selection of the base image. In my example I am using the Microsoft base image below, but you could use any OS capable of running Java:

mcr.microsoft.com/windows/servercore:ltsc2019

Last but not least, once you have a Dockefile is to build a docker image using the commands below:

docker build -t <imagename:imagetag> .
docker run -p 80:80 <imagename:imagetag>