Spring Boot + RESTful Tutorial

Gustavo Ponce
4 min readNov 10, 2016

--

In this tutorial I will show you how easy is to setup and execute a Spring Boot project using the following tech stack:

  • Spring Boot(1.4.2)
  • Maven (3.3.9)
  • Eclipse (Neon, 4.6.0)
  • Java 8
  • Packaging (JAR), remember… Make JAR no WAR ;)
  • JPA
  • RESTful
  • H2
  • Actuator

Project Creation

First we will use a very useful Spring initializer in order to create our maven project with the dependencies listed above.

  1. Go to → https://start.spring.io/
  2. Leave everything as it is and select the following dependencies: Web, JPA, H2 and Actuator.

Click on Generate Project button and it will download a zip file (demo.zip) with our maven project.

Import Project into Eclipse

  1. Unzip the zip file.
  2. Import into Eclipse as “Existing Maven Project
  3. Choose the root directory of the project generated (where the pom.xml file is located) and click on Finish.

It will display the next project structure.

pom.xml file

Model Creation

Let´s create our model class called Employee (Entity class).

Data Layer (JPA Repository)

Now we are going to create our JPA repository using the Employee class.

Service Layer

Now let´s create our service layer(interface and implementation). We will inject the repository into the service.

Interface

Implementation

Controller Layer

Now it´s time to create our controller layer, we will inject our service into the RestController layer.

Initialize Sample Data

Now we need to initialize our database (H2) with sample data using the interface CommandLineRunner. This interface is used to indicate that a bean should run when it is contained within a SpringApplication.

Note: out-of-the-box Spring Boot will setup the database (H2) configurations.

Launching the Application from Eclipse

Right click on DemoApplication.java → Run As → Java Application

Expected output

2016–11–10 10:28:11.604 INFO 2232 — — [ main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)

2016–11–10 10:28:11.710 INFO 2232 — — [ main] com.example.DemoApplication : The sample data has been generated

2016–11–10 10:28:11.713 INFO 2232 — — [ main] com.example.DemoApplication : Started DemoApplication in 12.155 seconds (JVM running for 13.013)

Testing RESTful calls

Finally it´s time to execute our RESTful calls implemented.

Get All Employees → http://localhost:8080/employee

Output

[{"id": 1,"name": "Gustavo","lastName": "Ponce","active": true},{"id": 2,"name": "John","lastName": "Smith","active": true},{"id": 3,"name": "Jim ","lastName": "Morrison","active": false},{"id": 4,"name": "David","lastName": "Gilmour","active": true}]

Get One Employe by Id → http://localhost:8080/employee/2

Output

{"id": 2,"name": "John","lastName": "Smith","active": true}

Note: If you are using google chrome I recommend you install JSONFormater in order to have a more readable output.

Metrics

Maybe you are wonder… Why did we add Actuator as dependency?

Here the answer…. Actuators allow us to consult our RESTful metrics in the next way.

http://localhost:8080/metrics

output

{"mem": 75818,"mem.free": 2873,"processors": 4,"instance.uptime": 172536,"uptime": 184464,"systemload.average": -1,"heap.committed": 45172,"heap.init": 16384,"heap.used": 42298,"heap": 253440,"nonheap.committed": 31008,"nonheap.init": 160,"nonheap.used": 30647,"nonheap": 0,"threads.peak": 17,"threads.daemon": 15,"threads.totalStarted": 22,"threads": 17,"classes": 9497,"classes.loaded": 9497,"classes.unloaded": 0,"gc.copy.count": 85,"gc.copy.time": 535,"gc.marksweepcompact.count": 3,"gc.marksweepcompact.time": 206,"httpsessions.max": -1,"httpsessions.active": 0,"datasource.primary.active": 0,"datasource.primary.usage": 0,"gauge.response.employee": 512,"counter.status.200.employee": 1}

http://localhost:8080/health

output

{"status": "UP","diskSpace": {"status": "UP","total": 498659852288,"free": 431055863808,"threshold": 10485760},"db": {"status": "UP","database": "H2","hello": 1}}

Note: For production environments you must implement some kind of security (like Spring Security) in order to avoid displaying this information to everybody.

Launching the Application from the Command Line

If you want to launch the application using the jar file generated, please take a look at the following steps.

  1. Open your command line
  2. Go to the root director project (where the pom.xml file is located)
  3. Type mvn clean
  4. Type mvn install
  5. Go to the target folder
  6. Type java -jar demo-0.0.1-SNAPSHOT.jar
  7. Verify your RESTful calls.

http://localhost:8080/employee

http://localhost:8080/employee/1

http://localhost:8080/metrics

http://localhost:8080/health

That´s all folks, as you can see Spring Boot is a great way to implement RESTful web services.

if you have any question or feedback don’t hesitate to write your thoughts in the responses section.

GitHub Repository Code

--

--