Build a Spring Boot Web Service Application

In this story, I will build a sample Spring Boot application that serves as a simple RESTful Web Service.

If you want to skip to the result, you may check the GitHub repo.

Before reading, you may want to confirm you have installed JDK 11 on your computer.

Download a Template

Open with VS Code and Setup the IDE for Spring Boot

Open the, you may see that VS code automatically ask you for installing the Java extension. Click Install for the Java extension.

After installing the Java extension, the Java Overview page will be shown. There are many useful extensions for Spring Boot development in VS Code. For now, I will install the Spring Boot Extension Pack.

Next, we install the Java Code Generators which will be useful for Generating code templates for Java.

Open the Extensions tab in the VS Code and enter . Install this extension in your VS Code. It will be useful for creating the properties setter and getter and constructors and more.

Next, you may install OpenJDK 11 if you haven’t installed it yet. Go to “Configure Java Runtime”.

Select the version of JDK you want to download. Here, I select OpenJDK 11 with Hotspot JVM.

An MSI file should be downloaded. Execute it for installing the JDK.

Reopen the VS code.

The VS Code will scan the project folder and ask if you want to import the Java projects it found. Click Yes for the demo project.

After loading, you may see your project in the Spring Boot Dashboard. You can start the App by playing the start button.

If you face any problems, please confirm your JAVA_HOME environment variable is set up properly.

Finally, for the Key Bindings part, I am going to use Eclipse Key Bindings.

For now, you have set up most of the settings for VS code.

Develop the Application

We will add an actuator package for creating a health check endpoint for our web service. Insert implementation 'org.springframework.boot:spring-boot-starter-actuator' inside the dependencies section.

Your build.gradle should look like this.

we will create a folder inside the com.mhho.demo package and name it model .

Create a file named inside the model folder. VS Code will automatically create the Java class structure for us. Following is the result I have got.

Next, we create two String variables in the Post model.

package com.mhho.demo.model;public class Post {    private String postId;    private String postBody;}

We can create the constructor and variables getters and setters by the Java Code Generator extension installed.

The resulting code should be similar to this.

Next, we are going to the controller and service layer.

Create controller and service folder under the com.mhho.demo package.

Create in the service folder. We add the Service annotation in front of the class to indicate the class is a service component.

Create a method for returning the post based on postId.

Then we are going to create the in controller folder. It will provide a RESTful API for getting the post.

After that, start your application with the Spring Boot Dashboard. Enter http://localhost:8080/posts/1 in your browser. You can get back the post detail object.

For checking the application’s health status, you may enter http://localhost:8080/actuator/health in your browser.


Share stories in my journal on programming and hobbies!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store