Create a Simple Web Service Project with Spring Boot

With this blog post, I am going to share with you how to quickly create a very simple Spring MVC Web Service project and then build and run it with Maven. In my following blog posts, I am going to enhance this Web Service project and add more functionality to it. And to begin with, I will create a simple web service project and make it run and display a simple plain text message.

There are a few of very simple ways to create a new Spring Boot Web Service project. You can create a new Spring Boot Project using:

  • Spring Boot Initializr,
  • Spring Tool Suite (STS for short)
  • or you can create it using your favorite Java editor and then add the needed dependencies into pom.xml file. Then build and run this project using Maven.

The simplest way of all, in my opinion, is to use the Spring Initializr page.

Create Web Service using Spring Initializr

This section also contains a video tutorial on how to create a new project with Spring Initializr.

To create a new Spring Boot Web Service project using Spring Initializr,

You should see a similar interface on your screen. Over time the page design and structure might change. But at the time of writing this blog post it looks this way:

Spring Initializr Page

Now fill in this form with the following details:

Group – provide the root package name of your project. For example, I am going to build a RESTful Web Service API for my mobile app and the package name for my project will be: com.appsdeveloperblog.ws.api. Where the com.appsdeveloperblog is the reverse domain name of my website.

Artifact – Provide project name here. In my case, it will be mobile-app-ws

For the Dependencies, at this moment, provide Web. The Web Spring Boot dependencies will add the support for needed libraries to make your web service build and run with Tomcat Servlet Container. So the filled in form for me now looks this way:

Spring Initializr Page Filled in

 

Click on Generate Project button. This will generate the Web Service project for you and will make it download to your computer in a ZIP archive. Unzip the project and move it to your preferable directory.

New Project with Spring Initializr. Video Tutorial.

Creating RestController

For the project, we have just created using Spring Initializr to be able to work as a Web Service and be able to handle HTTP requests and respond back with an HTTP Response we need to create a new class and annotate it with a few special annotations. Have a look at the very simple class I have created below:

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 *
 * @author skargopolov
 */
@RestController
@RequestMapping("users")
public class UserController {

    @GetMapping("/status/check")
    public String status() {
        return "working";
    }
}

Please note that I have annotated this class with @RestController and @RequestMapping annotations.

The @RestController annotation will register this class with a framework to make sure it is available when needed to handle HTTP Request. And the @RequestMapping annotation will make the methods in this Resource class be available when a request is sent to /users URL request path. Like for example:

http://localhost:8080/users

Please note that a method in the above Controller class is also annotated with @GetMapping annotation.

The @GetMapping annotation will register this method with a framework and will trigger this method when HTTP Get request is sent to a /users/status/check URL path. For example:

http://localhost:8080/users/status/check

The method will return back to the calling client a very simple response “working”.

Build and Run Web Service

You can use the following Maven command to build your project.

maven install

to run this web service using the built in Tomcat Servlet Container use the following command:

java -jar target/mobile-app-ws-0.0.1-SNAPSHOT.jar

where the mobile-app-ws-0.0.1-SNAPSHOT.jar is the name of the JAR file which was generated by maven install command and was placed inside of the target folder.

Sending HTTP GET Request

The above created RestController class contains a method with a @GetMapping annotation and which means that this method will be executed when our RestController receives HTTP GET request.

There are many ways to send HTTP Get requests and the simplest is probably by simply opening the target URL in a browser window. For example, if you open the below URL in the browser window, you will trigger the method we have defined:

http://localhost:8080/users/status/check

Using CURL 

Or you can use this simple CURL command to send HTTP Get Request from your command line in the terminal window:

curl -i -X GET "http://localhost:8080/users/status/check"

I hope this blog post was of some good value to you.

Now you know how to quickly create a very simple Spring MVC Web Service using Spring Boot and Spring Initializr.

Check out the below video courses on building RESTful Web Services with Spring Boot. One of them might be the one to take your skills to the whole new level!

Happy learning!

Learning RESTful Web Services Development