Articles /Pin to ProfileBookmark

The Beginners Guide to REST Applications in Spring Boot

Dec 27, 2022  •  @harrydrummond  •  4,000
Copy linkTweet this

Spring Boot is a popular framework for building web applications in Java. It makes it easy to create stand-alone, production-grade Spring-based applications that you can “just run”.

By the end of this article, you will know how to quickly set up a website using Spring Boot.

Prerequisites

  • Java 8 or higher
  • Apache Maven
  • We will also not cover setting up a Java application with maven as it is expected you know how to do this already.

What is REST?

REST is a way to build web services that allows different computer systems to communicate with each other. It uses HTTP methods to perform operations on resources and HTTP response codes to indicate the success or failure of a request. RESTful web services can be written in any language and are easy to use because they rely on a simple and standard way of exchanging data.

Let’s get started.

To get started with Spring Boot, load up your maven project in your editor / IDE of choice and open the pom.xml file in the root directory of the project. You should now add the following dependency:

<dependencies>
 <dependency>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-web</artifactId>
 </dependency>
</dependencies>

This will add all the necessary dependencies for building a web REST application with Spring Boot.

Create the Spring Boot Entry Point

When we run our application, we will need to enable the Spring Boot auto-configuration feature. We do this by annotating the Main class with @SpringBootApplication and calling the SpringApplication#run function.

package com.example.myproject;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class MyApplication {
 public static void main(String[] args) {
 SpringApplication.run(MyApplication.class, args);
 }
}

Create a Controller

Now, create a controller class to handle HTTP requests. A controller is a Java class that is annotated with @RestController. This annotation indicates that the class will handle HTTP requests and return a response:

package com.example.myproject.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController { 

 @GetMapping("/") 
 public String sayHello() { 
 return "Hello, World!"; 
 }
}

In this example, we have created a single endpoint that listens for GET requests at the root path (/). When a request is received, the sayHello() method is called and the string “Hello, World!” is returned as the response.

Run the Application

Finally, we can run the application using the mvn spring-boot:run command. This will start the embedded Tomcat server and make the website available at http://localhost:8080.

That’s it! You have successfully set up a website using Spring Boot. You can now start building and make a simple REST get call.

Back-endJava
×

Success!

Help @harrydrummond spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with Github
about: ({
version: 0.1.7 BETA 2.4,
whats_new: article editor v2,
up_next: tip code demos,
coming_soon: embeddable tipping,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh

version: 0.1.5,
notes: added pins + new badge

version: 0.1.4,
notes: home refresh + tools
)...
recent_tips: (
tipper: @JohnDavis,
amount: 250 SATS,
source: article

tipper: @Megan0910,
amount: 1000 SATS,
source: article

tipper: @JohnDavis,
amount: 250 SATS,
source: article
)...