Spring Boot in Action: Part2: Project Setup

We continue our blog series building Kotoby (check Part 1 ). In this post we will build the application skeleton and pick the components we will need to build it.

Kotoby Components Selection

  • Servlet Container: Tomcat 8.0
  • Web Framework: Spring MVC with thymeleaf as the template engine
  • Data Access: JPA with Hibernate
  • Database: HSQL (embedded in memory) (we will change it later)
  • Security: Spring Security + Spring Social (for social login)

Spring Initializr

One of the most dreaded tasks in building java applications is the initial project maven setup (or gradle if you prefer that). I don't know about others but for me building new pom.xml files from scratch has always been a major pain for me. Luckily, if you are going to use Spring Boot, there is a website http://start.spring.io/ maintained by the people at Pivotal which helps a lot.

The website allows you to simply select the components that you need from spring boot and it will create a maven project for you to use.

Enough Talk let's start working

Ok let's see things in action. Go to http://start.spring.io/ and enter the following:

  1. Group: com.ashnab.kotoby
  2. Artificat: kotoby-web
  3. Name: Kotoby
  4. Description: A sample application for Spring Boot
  5. Package Name: com.ashnab.kotoby.runner
  6. Styles: select Acutator, JPA, Security, Thymeleaf, Web
  7. Type: Maven Project

Clicking on Generate will download a zip file containing the project setup. Extract the zip file to a directory and you shall see a file structure like this:

your-directory-name | |-> pom.xml (Maven root configuration file) |-> build.gradle (Gradle build file) |-> src (directory holding source files) | |-> main (contains source files) |-> java ( java packages and classes) |-> resources (contains other resources) |-> application.properties (application configurations) |-> test (contains test files)

The next step is to import the project in your IDE of choice. I leave the choice of IDE to the reader.

A bit more configurations

After loading the project in your IDE of choice. Before going a head and running the application, there a couple of changes that we will be doing.

  1. Use Tomcat 8:
    Spring Boot by default uses Tomcat 7, in order to upgrade to Tomcat 8 all that is needed is to add the following property in the section of the pom.xml file

  2. Add HSQL db dependency:
    Spring Boot does not pick a certain database by default. It will automatically configure whatever database dependency that have on the classpath. Initially we will be using HSQL db so let's add the dependency in the section in pom.xml
    <dependency> <groupId>org.hsqldb</groupId> <artifactId>hsqldb</artifactId> <scope>runtime</scope> </dependency>

  3. Disable Templates Check:
    Thymeleaf support in Spring Boot does a check at startup looking for at least one .htm files in the templates directory. This behavior can be turned off by adding this line to application.properites

Let's Take this thing for a spin

To run the application, you can either use maven from the command line by navigating to the application root directory and running the command
mvn spring-boot:run

Or simply by running the main method inside the Application.java file using your IDE.

If you did all the configurations correctly, you should see a lot of log messages in your console once they are done, open your browser and navigate to localhost:8080. You should be greeted with security prompt to enter your user name and password. But where are those?

Since we added spring-security earlier, Spring Boot automatically secures all application end points with Basic Http Authentication with a default user name user and randomly generated password. The password will be logged in the console at the application startup. It will be written out like this
Using default password for application endpoints: random-hash-here

Enter the credentials and you shall get a nice 404 page (we didn't configure any web mappings yet).

Wrap Up

With this we have completed our project setup and are ready to start building our features which we will be doing in the rest of this blog series. A completed sample of the work we did in this blog post is available here

Mohamed Osman

Read more posts by this author.