Building a RESTful Web Service in Spring Boot

In this article, we will learn how to build a RESTful web service with Spring Boot. We will build a service that will accept HTTP GET requests and respond with a JSON representation.

Building RESTful Web Services with Spring Boot is very easy as it provides good support for REST services. RESTful Web services are really popular these days for enterprise applications.

Starting with Spring Initializr

For all Spring Boot applications, we should start with the Spring Initializr. The Initializr offers a fast way to pull in all the dependencies we need for an application. Please see the below setup process.

You may like: Most Useful JavaScript Array Functions

In this article, we have started with the Maven build project and we can get all dependencies from the Spring Initializr. The following listing shows the pom.xml file that is created when you choose Maven:

We can also get a Gradle build file with all necessary dependencies. The following build.gradle file will be created when you choose Gradle:

plugins {
id 'org.springframework.boot' version '2.4.2'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}

group = 'net.codinghub'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

repositories {
mavenCentral()
}

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
testImplementation('org.springframework.boot:spring-boot-starter-test')
}

test {
useJUnitPlatform()
}

Create a Resource Representation Class

We have set up the project and build the system. Now we can create our own service. Let's start.

The service will handle GET requests for /rest-service, The GET request should return a 200 OK response with JSON in the body that represents a rest service. It should display the following output:

{ 
"id": 1,
"content": "Coding Hub"
}

We have created RestResponse resource representation class with fields and accessors for the id and content data.

You may like: Deno — JWT Authentication

Create a Resource Controller

To building a RESTful application, we have to use @RestController annotation. As we know HTTP requests handled by a controller.

@GetMapping annotation ensures HTTP GET requests onto specific handler methods. It is a composed annotation that acts as a shortcut for @RequestMapping(method = RequestMethod.GET).

We use Spring @RestController annotation, which marks the class as a controller where every method returns a domain object instead of a view. It is shorthand for including both @Controller and @ResponseBody.

ResponseEntity represents the whole HTTP response with status code, headers, and body. As a result, we can use it to fully configure the HTTP response.

Build an executable JAR

We can run the application from the command line with Gradle or Maven. We can also build a single executable JAR file that contains all the resources. Building an executable JAR helps us to deploy the code across different environments.

As we are using Maven build, we can run the application by using ./mvnw spring-boot:run. Alternatively, you can build the JAR file with ./mvnw clean package and then run the JAR file, as follows:

java -jar target/rest-service-0.0.1-SNAPSHOT.jar

If you use Gradle, you can run the application by using ./gradlew bootRun. Alternatively, we can build the JAR file by using ./gradlew build and then run the JAR file:

java -jar build/libs/rest-service-0.0.1-SNAPSHOT.jar

You may like: MongoDB commands and methods

Summary

I hope this article will help you a lot to start a RESTful Spring Boot application. If you start from scratch then follow the article very carefully and get in touch with us if you have any problem.

Originally published at https://www.codinghub.net.

Coding Hub — Learn & Build Your Career

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