Spring Boot, Spring Cloud AWS and ElasticBeanstalk — Part 1 — The Application

Fatih Dirlikli
The Clone
Published in
6 min readSep 14, 2015

Recently I was playing around with Amazon Web Services on developing and deploying a java web application which utilizes some of the services provided by AWS like RDS and EC2. What I noticed during my efforts is, there is not much online resources which will help you to quickly ramp up and get ready for deployment. This pushed me to prepare this blog post which will briefly describe the process as a ‘how to’ guide.

The Application

As this is a trivial application and the intention is to only see how easy it’s to prepare and deploy an application on AWS platform there is not much logic involved with it. I decided to develop a Restful back-end application without a web UI which serves a basic restful service consisting of classic GET, POST etc. methods. A MySQL instance backed by AWS RDS is used as the persistence store to see how well it plays with AWS services.

As my intention was to get ready for a basic web application deployment quickly I looked for the tool set which would ease and quicken the process as much as possible. Below is the list I came up with:

Spring Data JPA

I decided to use Spring Data JPA as Spring Repositories minimizes the effort for creating your DAO layer for common CRUD operations with minimal code and configuration. I’m not going to go into deep details about Spring Data JPA and just going to settle for saying that Spring Data JPA plays really well with Spring Data Rest on automatizing the creation of Restful Services based on JPA entities and repositories.

Spring Data Rest

Its official documentation defines the goal of the Spring Data Rest as “The goal of the Spring Data REST project is to provide a solid foundation on which to expose CRUD operations to your JPA Repository-managed entities using plain HTTP REST semantics.” So as I said with a few clicks and a few annotations here and there, it’s trivial to get your Restful Service ready in minutes.

You can find the details about Spring Data Rest here.

Spring Cloud for AWS

Any Java project that wants to leverage AWS platform services should depend on the SDK (AWS SDK for Java) provided by AWS. AWS SDK for Java provides different APIs for different services that’s required to be used. Spring Cloud for AWS can be thought as a wrapper around AWS SDK for Java which while hiding the complexity of the SDK also let’s developers to code in a more “Springy” way. Currently supported AWS Services are SNS, SQS, ElastiCache, CloudFormation, RDS and S3.

Spring Boot

Spring Boot is the glue of the application. It bring together all the components and configures them automatically depending on the classes on the classpath or spring beans defined by the application or even based on the properties in you application.properties file. It helps you to get your application running with minimal or no configuration which speeds up the process extremely.

Getting Your Hands Dirty

After enough theory it’s time to see some real code. Below sections will be describing how above tool set is used throughout the project.

Application Structure

Below you can see the directory structure of the project. As already stated this is a trivial project to test AWS platform with a Java web project. There is not much to see in the project directories. We have 3 Java files under src/main/java directory which puts together the actual application. There is also a application.properties file under src/main/resources which stores the application configuration for use of Spring Boot. The last file is the pom.xml as this is a Maven project which configures the project from Maven perspective. Contents of each file will be detailed in the upcoming sections.

Directory

pom.xml

This is not the place for describing the Maven details. Below I’ll only be highlighting the project dependencies.

Below you can see the list of the dependencies of the project. First two dependencies are for Spring Data Rest and Spring Data JPA followed by the mysql connector dependency required for any project that needs to interact with a MySQL database.

You should pay special attention to fourth dependency which is spring-boot-starter-tomcat. By default Spring Boot brings in a embedded Tomcat for every web project that you implement. But as we’re going to be deploying to an AWS EC2 instance and the Tomcat is going to provided by AWS platform, we should explicitly add the Tomcat dependency and tell Maven that it’s a provided dependency and should not be included in the resulting artifact.

Fifth and sixth dependencies describe themselves and they bring in the Spring support for AWS Context and AWS RDS.

Application.java

This is our main application class. I’ll be summarizing the key points. To give our application web behaviour Application class must extend SpringBootServletInitializer class. Knowing this we can continue with the annotations used on the class:

@SpringBootApplication: While this annotation marks the Application class as a Spring Boot Application actually all it does is to bring together 3 important spring annotation which are @ComponentScan, @EnableAutoConfiguration and @Configuration.

@EnableJpaRepositories: This annotation enables the Spring Data Jpa repositories support. Without any need for additional configuration it configures your datasource, entity manager and transaction manager. It also scans for any spring repository implementation in the same package with the annotated class. In this example it scans for the repository implementations in the org.fatih.dirlikli.aws.restfulservice.restserver package as this is the package for Application class. If your repositories are in a different package than the Application class you can use the basePackages property to configure the packages for scanning the repository implementation.

@EnableRdsInstance annotation enables the support for AWS RDS support which is the relational database service provided by Amazon Web Services platform. There are different choices provided by AWS as DB implementation including MySQL, Oracle or PostgreSQL. In the sample application we’ll be using a MySQL database. This is all the code that you need to write for integrating with AWS RDS service. Parameters of the annotation is self-explanatory so will not be detailed here.

@EnableContextCredentials annotation is used to configure the security portion while communicating with AWS platform. Its two properties accessKey and secretKey are provided by AWS platform when you create an account or user and embedded into the application for successful authentication and authorization for using AWS resources.

Customer.java

There is nothing interesting about the Customer class. As shown below this is a basic JPA entity class with properties and corresponding getters/setters.

CustomerRepository.java

CustomerRepository is our DAO interface. Below is the only code that you should write to obtain basic CRUD functionality. And yes the below code just works as is. When I said Spring Repositories minimizes the code for implementing your DAO classes I meant it. You don’t have to write any code other than extending an existing interface and Spring generates the necessary implementation for you on the fly. CustomerRepository interface is extending the PagingAndSortingRepository interface because I wanted to have paging and sorting support for my entity. There are various alternatives like CrudRepository or JpaRepository. Just check the Spring Data JPA documentation to find out what’s suitable for you.

You should be wondering what the @RepositoryRestResource annotation is for. It’s what transforms our repository into a REST resource. Once a Spring Repository is annotated with @RepositoryRestResource annotation Spring Data Rest generates all the necessary code to transform your repository into a REST resource at runtime. You can use standard HTTP GET/POST/PUT/PATCH etc. requests to display and manipulate your entities.

application.properties

Spring Boot reads the application.properties file under src/main/resources/ directory at bootup for global application configuration. The contents of the file in our case is below. As each property is self explanatory I’ll not be describing them one by one. All of the properties used are pre-defined by Spring Boot and there is nothing else you should do as Spring Boot reads and uses them automatically. On the other hand you can define your own properties in this file (if required) and inject them into your beans with @Value annotation for further use.

Conclusion

This concludes the first part of a two part blog series. We talked about some some Spring projects and how they can ease our life while developing a restful web application ready for deployment to Amazon Web Service platform. We also implemented a sample application using these technologies.

In the second part of these series I’ll be talking about how to deploy our sample application to AWS platform using AWS ElasticBeanstalk.

--

--

Fatih Dirlikli
The Clone

Full stack software engineer. Mostly Java and Angular. Learning Go and Phyton.