Part 5: Running the application

Erwin Alberto
3 min readJan 22, 2018

--

Before we run the application, I would like to talk about Swagger and how to load runtime properties into our application.

Swagger

As mentioned earlier, using Swagger gave me the ability to not only show my REST endpoints in a single document but it also allowed me to easily interact with endpoints without having to use curl or a third party tool like Postman. To setup Swagger in this application, I wrote a SwaggerConfig.

package com.erwindev.openpayment.config

import org.springframework.beans.factory.annotation.Value
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import springfox.documentation.builders.ApiInfoBuilder
import springfox.documentation.builders.PathSelectors.regex
import springfox.documentation.builders.RequestHandlerSelectors
import springfox.documentation.service.ApiInfo
import springfox.documentation.service.Contact
import springfox.documentation.spi.DocumentationType
import springfox.documentation.spring.web.plugins.Docket
import springfox.documentation.swagger2.annotations.EnableSwagger2

/**
* Created by erwinalberto on 1/5/18.
*/
@Configuration
@EnableSwagger2
class SwaggerConfig {

@Value("\${info.app.name}") lateinit var appName: String
@Value("\${info.app.description}") lateinit var appDescription: String
@Value("\${info.app.contact}") lateinit var appContact: String
@Value("\${info.app.email}") lateinit var appEmail: String

@Bean
open fun api(): Docket = Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("com.erwindev.openpayment.controller"))
.paths(regex("/api/openpayment/v1.*"))
.build().apiInfo(apiInfo())

fun apiInfo(): ApiInfo {

var contact: Contact = Contact(appContact, "", appEmail)

return ApiInfoBuilder()
.title(appName)
.description(appDescription)
.version("1.0")
.termsOfServiceUrl("Terms of Service URL")
.contact(contact)
.license("License of API")
.licenseUrl("API License URL")
.build()

}

}

There a few things to note about this class.

  • @Configuration annotation tells Spring that the class is a source of bean definitions.
  • @EnableSwagger2 tells that the class that Swagger 2 is enabled through out the application
  • The apiInfo function returns an ApiInfo class that will allow the following information to show up in the Swagger page: Title, Description, Version, Terms of Service URL, Contact info via the Contact object, License description and License URL.
  • The api function tells Swagger which REST endpoints to include in the Swagger documentation.

Application Settings

In order to pass pass properties to the application during runtime, I defined an application.properties file that contain all these property values. This file is located in /src/main/resources directory. By default, Spring will automatically detect and load this file. For example, I defined the server.port property to be 8998. So, when I bring up the application, I can access it from http://localhost:8998/swagger-ui.html.

To get information from the application.properties file, I used a couple of ways to load the properties in my application. First, I used the @Value annotation. You can see an example of this in the SwaggerConfig class. Second, I had to write a class and annotate it as a configuration. In this class, I am loading properties that are prefix “erwindev”.

package com.erwindev.openpayment.util

import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.boot.context.properties.EnableConfigurationProperties
import org.springframework.context.annotation.Configuration

/**
* Created by erwinalberto on 1/5/18.
*/

@Configuration
@EnableConfigurationProperties
@ConfigurationProperties(prefix="erwindev")
class ApplicationSettings {

lateinit var apiVersion: String
lateinit var paymentFileName: String
}

Here is the contents of the application.properties file.

server.port=8998

erwindev.api_version = local
erwindev.payment_file_name=datafiles/OP_DTL_GNRL_PGYR2016_P06302017_small.csv

db.schema_file=db_schema.sql

info.app.name=OpenPayment API
info.app.description=Provides a way to view OpenPayment data
info.app.version=1.0.0
info.app.contact=Erwin Alberto
info.app.email=erwin@erwindev.com

Running the application

First, clone the repo.

$ git clone https://github.com/erwindev/healthcare-openpayment-data.git

Then, build the application.

$ gradle assemble

Lastly, run the application.

$ java -jar build/libs/healthcare-openpayment-data-1.0.jar

To access the application, you can go to your browser and pull up the Swagger page

Summary

If you are looking for an alternative to Java and Groovy, Kotlin is a worthy replacement. It leverages the power of the Java ecosystem with improved safety and conciseness of code. Combining it with Spring Boot provides an excellent and easy way of writing Java microservices. I hope this tutorial gave you a good basic overview on how to write Spring Boot Microservices using Kotlin.

You can find the code this GitHub repo.

--

--