DEV Community

loading...
Cover image for Build a Spring Boot ReST API

Build a Spring Boot ReST API

fastcode-inc
A low-code platform for Angular/Java developers.
Originally published at getfastcode.com ・3 min read

In this blog article series, we will incrementally build a Spring Boot ReST API. We will start from scratch so that readers who are new to Spring Boot can easily follow along with us.

In this first article, we will develop a simple Spring Boot application that prints “Hello World” to the console.

Step-1:

Go to the Spring Initializr website (https://start.spring.io/) and create/download a Spring Boot project. Use the settings shown in the image below:

Step-2:

Unzip the zip file. The zip file will be unzipped into demo directory

Step-3:

Ensure that you have Maven and JDK 1.8 installed on your computer. Also ensure that you add them to your PATH so that you can access the commands java and mvn from your command window.

You can get them from the following location:

JDK 1.8: https://adoptopenjdk.net/variant=openjdk8&jvmVariant=hotspot

Maven: https://maven.apache.org/download.cgi

Step-4:

From a command window, go to the directory demo, and compile and package the application using the following command:

mvn clean package

You will see that the application is compiled and a jar file named demo-0.0.1-SNAPSHOT.jar is created in a new directory named target.

When you do a maven clean, this target directory (along with all the artifacts in this directory) is deleted. The maven package step both compiles the application and packages it in a jar file. To further understand the different maven lifecycle steps, please refer to this webpage.

To run this application, execute the following command from the command line:

java -jar target\demo-0.0.1-SNAPSHOT.jar (if you are using windows) or java -jar target/demo-0.0.1-SNAPSHOT.jar (if you are using Mac or Linux)

This will run the application. However, this application does not do anything and therefore runs and exits.

Step-5:

Now let’s look at the only source file (*.java) we have in this application - DemoApplication.java which is located at demo/src/main/java/com/example/demo/DemoApplication.java.

package com.example.demo;

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

@SpringBootApplication
public class DemoApplication {

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

}
Enter fullscreen mode Exit fullscreen mode

First, notice that there is a main method. This method doesn’t do anything except to run the application. Also, notice the @SpringBootApplication annotation on the class. This annotation is equivalent to three separate Spring Boot annotations:

@SpringBootApplication = @Configuration + @ComponentScan + @EnableAutoConfiguration

You can learn what these annotations mean at the Spring website.

Step-6:

Now, let’s write a method that will print “Hello World!” to the console. Here we go:

package com.example.demo;

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

@SpringBootApplication
public class DemoApplication {

 public static void main(String[] args) {
     SpringApplication.run(DemoApplication.class, args);
 HelloWorld();
  }

 private static void HelloWorld() {
     System.out.println("Hello World!");
  }

}
Enter fullscreen mode Exit fullscreen mode

Notice the method Hello() defined in the DemoApplication class. It’s defined as static - otherwise, you will get the error “Non-static method 'HelloWorld()' cannot be referenced from a static context”.

Now, you can re-run the maven command, mvn clean package, and then run the program using java -jar target\demo-0.0.1-SNAPSHOT.jar (if you are using windows) or java -jar target/demo-0.0.1-SNAPSHOT.jar (if you are using Mac or Linux).

You will see a message “Hello World!” printed to the console window.

That’s all for this article. In the next article, we will extend this application and build an API that can be called from external systems.

Discussion (0)