--

Spring Boot makes it easy to create Spring-powered, production-grade applications and services with absolute minimum fuss. It takes an opinionated view of the Spring platform so that new and existing users can quickly get to the bits they need.

In this tutorial, i am going to walk you through the basics of starting a spring boot restful service application with all the necessary integrations like PostgreSQL database, swagger, Liquibase.

My goto place for bootstrapping a spring boot application is the Spring Initializer website https://start.spring.io/

To create a Spring boot 2.0 application, make sure to select the latest release (currently that’s 2.1.0 M1). After that, select the following dependencies:

  1. Web: Full-stack web development with Tomcat and Spring MVC
  2. Security: Secure your application via spring-security
  3. PostgreSQL: PostgreSQL JDBC driver
  4. JPA: Java Persistence API including spring-data-jpa, spring-orm and Hibernate
  5. Liquibase: Liquibase Database Migrations library
  6. Springfox-swagger2: implementation of the Swagger 2 specification.
  7. Springfox-swagger-ui: Swagger UI is a built-in solution which makes user interaction with the Swagger-generated API documentation much easier.

Clicking on “Generate Project” will download project to your download directory. So you can extract the downloaded project and open it as a new project with your favorite IDE. I love IntelliJ IDEA, you can download it from https://www.jetbrains.com/idea/download/

The Pom.xml file does not come with the swagger dependencies, so you have to add them yourself.

Adding Springfox-swagger2 and Springfox-swagger-ui maven dependencies:

<dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.7.0</version></dependency><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>2.7.0</version></dependency><dependency><groupId>io.jsonwebtoken</groupId><artifactId>jjwt</artifactId><version>0.7.0</version></dependency>

Right now our project can’t run because we need to configure the data source and liquibase migration tool.

Datasource Configuration

To configure your datasource as postgreSQL database, open up your application properties file and paste these property settings in it :

spring.datasource.driver-class-name=org.postgresql.Driverspring.datasource.url=jdbc:postgresql://localhost:5432/<database_name>spring.datasource.username=<database_username>spring.datasource.password=<database_password>spring.jpa.hibernate.ddl-auto=none // This settings is optional, it tells hibernate to hands off the “Data Definition Language” operations to liquibase  i.e Creating databases, altering databases, dropping databases etc

Liquibase Configuration

In the resource folder, create db/changelog directory. Inside the changelog sub-directory, create a new XML (or YAML, JSON) file named “liquibase-changelog.xml”. This is a changelog file, where all your changesets will live.

NB: my changesets are created inside an includes folder which is a sub-directory of changelog folder.

Add the liquibase config settings in your property file:

spring.liquibase.change-log= classpath:/db/changelog/liquibase-changelog.xml

At this point, your project can run successfully.

From here on we can create our plain old java object entity classes, and controllers. I will be creating a User entity, and User Controller so we can test our swagger integration.

For brevity, i will skip the codebase for controller, dto, service, repository, and entities.

You can get the source here: https://github.com/prestigegodson/spring-boot-starter-restful

Swagger Configuration Class

At this point, you can successfully open the swagger interface on your browser : http://<host>:<port>/<context-path-if-any>/swagger-ui.html

Next: will work you through JWT and stateless authentication and authorization in spring boot.

--

--

Godson Ositadinma
Getting Started With Spring Boot 2 + Swagger + liquibase

A results-driven, customer-focused, articulate and analytical Senior Software Engineer who can think “out of the box.”