REST APIs implemented using Spring Boot, in-memory database, embedded Tomcat, Swagger, JsonPath, Hamcrest and MockMVC
Switch branches/tags
Nothing to show
Clone or download
Pull request Compare This branch is 14 commits behind khoubyari:master.
Type Name Latest commit message Commit time
Failed to load latest commit information.
src correct the config file for Boot 1.3.x Mar 8, 2016
.gitignore initial commit Oct 5, 2014
LICENSE initial commit Oct 5, 2014 added REST API aspect (AOP) Jan 26, 2016
pom.xml upgrade to Boot 1.3.3; update config file Mar 7, 2016

Spring Boot "Microservice" Example Project

This is a sample Java / Maven / Spring Boot application that can be used as a starter for creating a microservice complete with built-in health check, metrics and much more. I hope it helps you.

How to Run

This application is packaged as a war which has Tomcat 7 embedded. No Tomcat or JBoss installation is necessary. You run it using the java -jar command.

  • Clone this repository
  • Make sure you are using JDK 1.7 and Maven 3.x
  • You can build the project and run the tests by running mvn clean package
  • Once successfully built, you can run the service by one of these two methods:
        java -jar target/spring-boot-rest-example-0.2.0.war
        mvn spring-boot:run -Drun.arguments=""
  • Check the stdout to make sure no exceptions are thrown

Once the application runs you should see something like this

2014-10-04 18:24:58.870  INFO 10190 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8090/http
2014-10-04 18:24:58.872  INFO 10190 --- [           main] com.khoubyari.example.Application        : Started Application in 6.764 seconds (JVM running for 7.06)

About the Service

The service is just a simple hotel review REST service. It uses an in-memory database to store the data. You can also do with a relational database like MySQL or PostgreSQL. If your database connection properties work, you can call some REST endpoints defined in on port 8090. (see below)

More interestingly, you can start calling some of the operational endpoints (see full list below) like /metrics and /health (these are available on port 8091)

You can use this sample service to understand the conventions and configurations that allow you to create a DB-backed RESTful service. Once you understand and get comfortable with the sample app you can add your own services following the same patterns as the sample service.

Here is what this little application demonstrates:

  • Full integration with the latest Spring Framework: inversion of control, dependency injection, etc.
  • Packaging as a single war with embedded container (tomcat 7): No need to install a container separately on the host just run using the java -jar command
  • Demonstrates how to set up healthcheck, metrics, info, environment, etc. endpoints automatically on a configured port. Inject your own health / metrics info with a few lines of code.
  • Writing a RESTful service using annotation: supports both XML and JSON request / response; simply use desired Accept header in your request
  • Exception mapping from application exceptions to the right HTTP response with exception details in the body
  • Spring Data Integration with JPA/Hibernate with just a few lines of configuration and familiar annotations.
  • Automatic CRUD functionality against the data source using Spring Repository pattern
  • Demonstrates MockMVC test framework with associated libraries
  • All APIs are "self-documented" by Swagger using annotations

Here are some endpoints you can call:

Get information about system health, configurations, etc.


Create a hotel resource

POST /example/v1/hotels
Accept: application/json
Content-Type: application/json

"name" : "Beds R Us",
"description" : "Very basic, small rooms but clean",
"city" : "Santa Ana",
"rating" : 2

RESPONSE: HTTP 201 (Created)
Location header: http://localhost:8090/example/v1/hotels/1

Retrieve a paginated list of hotels


Response: HTTP 200
Content: paginated list 

Update a hotel resource

PUT /example/v1/hotels/1
Accept: application/json
Content-Type: application/json

"name" : "Beds R Us",
"description" : "Very basic, small rooms but clean",
"city" : "Santa Ana",
"rating" : 3

RESPONSE: HTTP 204 (No Content)

About Spring Boot

Spring Boot is an "opinionated" application bootstrapping framework that makes it easy to create new RESTful services (among other types of applications). It provides many of the usual Spring facilities that can be configured easily usually without any XML. In addition to easy set up of Spring Controllers, Spring Data, etc. Spring Boot comes with the Actuator module that gives the application the following endpoints helpful in monitoring and operating the service:

/metrics Shows “metrics” information for the current application.

/health Shows application health information.

/info Displays arbitrary application info.

/configprops Displays a collated list of all @ConfigurationProperties.

/mappings Displays a collated list of all @RequestMapping paths.

/beans Displays a complete list of all the Spring Beans in your application.

/env Exposes properties from Spring’s ConfigurableEnvironment.

/trace Displays trace information (by default the last few HTTP requests).

Running the project with MySQL

This project uses an in-memory database so that you don't have to install a database in order to run it. However, converting it to run with another relational database such as MySQL or PostgreSQL is very easy. Since the project uses Spring Data and the Repository pattern, it's even fairly easy to back the same service with MongoDB.

Here is what you would do to back the services with MySQL, for example:

In pom.xml add:


Append this to the end of application.yml:

  profiles: mysql

    driverClassName: com.mysql.jdbc.Driver
    url: jdbc:mysql://<your_mysql_host_or_ip>/bootexample
    username: <your_mysql_username>
    password: <your_mysql_password>

      dialect: org.hibernate.dialect.MySQLInnoDBDialect
      ddl-auto: update # todo: in non-dev environments, comment this out:

  name: 'test profile:'

Then run is using the 'mysql' profile:

        java -jar target/spring-boot-rest-example-0.2.0.war
        mvn spring-boot:run -Drun.arguments=""

Attaching to the app remotely from your IDE

Run the service with these command line options:

mvn spring-boot:run -Drun.jvmArguments="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005"

and then you can connect to it remotely using your IDE. For example, from IntelliJ You have to add remote debug configuration: Edit configuration -> Remote.

Questions and Comments: