/    Sign up×
Articles /Pin to ProfileBookmark

The Beginners Guide to REST Applications in Spring Boot

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 GithubCreate Account
about: ({
version: 0.1.9 BETA 4.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...