This page allows you to start with the Hello World application using REST in Spring Boot. To develop this application, we will use JDK 8 and Maven. Here we will explain step by step procedure how to create this application, How to create controller that shows hello world in JSON format, how to compile and how to run the application to see the json message “Hello World” in the browser .
Start Spring Boot Hello World Project
Click on the “File “menu in the top left corner-> then click on the “New “menu-> then click on the “Spring Starter Project “button in the image below.
The link above will open a wizard “New Spring Starter Project” as a pop-up window. Change the Name to “SpringHelloWorldRest”. By default, the location will show in the work space. If you want to change to another location, un-check the “Use default location” location and select the location you want. Change Group, Artifact, Description, Package as shown in the picture. Leave the remaining field as the default value. To move next page, click on the “Next “button.
The window “New Spring Starter Project Dependencies” will open on the next page. There are two list, the “Available” list on the left shows all packages and the “Selected” list on the right is empty. Scroll down the list on the left until the end, expand “Web” from the list “Available”, then select the checkbox “Web”. Now the “Selected” list on the right side shows “Web”. To move next page, click on the “Next” button.
It will show “Site Info” on the next page as shown below. To create a default project, click “Finish” button.
Default Spring Boot Project File Structures
Now it creates the default Spring Boot Project. The directory structures are created as shown in the image below. The file “SpringHelloWorldRestApplication.java” was created in the source folder. This is the main method starting file. A default configuration file called “application.properties” is created in the resource folder. There are two folder named “static” and “templates” created in resources folder. A default test file with default code is created in the test folder “SpringHelloWorldRestApplicationTest.java”. In the root directory, a pom.xml file is created.
The pom.xml file creates dependency of web “spring-boot-starter-web”. Default test dependency is “spring-boot-starter-test”. The pom.xml file will contain the following code.
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.yawintutor</groupId>
<artifactId>SpringHelloWorldRest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SpringHelloWorldRest</name>
<description>Spring Boot Hello World Rest Project</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
The main Spring boot file “SpringHelloWorldRestApplication.java” is shown below. It contains a main method by default.
package com.yawintutor.helloworld;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringHelloWorldRestApplication {
public static void main(String[] args) {
SpringApplication.run(SpringHelloWorldRestApplication.class, args);
}
}
In the test folder, the default test java file “SpringHelloWorldRestApplication.java “contains the default test code.
package com.yawintutor.helloworld;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringHelloWorldRestApplicationTests {
@Test
public void contextLoads() {
}
}
Running the default Spring Boot Application
Open the file “SpringHelloWorldRestApplication.java” in the Project Explorer window by double-clicking on the file name.
You can use the Spring Tool Suite to run the Spring boot application in three ways.
- First option – Click on the “Run “menu in the menu-> Click on “Run As “-> click on “2. Spring Boot App”
- Second option – Right click on “SpringHelloWorldRestApplication.java” -> Click on “Run As” -> click on “Run Spring Boot App”
- Third option- Click in the toolbar on the “Run” button.
The output is displayed in the console window as below. By default, the default log statements are displayed in the console window. Since the tomcat server will start, it will run forever. Click the “stop” button to stop the application.
Create Hello World Rest Example
Create a java file “HelloWorldController.java” in the package “com.yawintutor.helloworld”. The path is “/src /main /java /com /yawintutor /helloworld /HelloWorldController.java”. As shown below, add the controller code.
Class HelloWorldController class contains firstPage() method. This method is invoked when user access from the browser with path “/HelloWorld”. This method returns Message object with a string “Welcome to Hello World!” that will be converted as JSON format.
package com.yawintutor.helloworld;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloWorldController {
@GetMapping("HelloWorld")
public Message firstPage() {
return new Message(1,"Welcome to Hello World!");
}
}
A Java bean class Message.java has been created to store message. Message class looks like below.
package com.yawintutor.helloworld;
public class Message {
private int id;
private String message;
public Message(int id, String message) {
this.id = id;
this.message = message;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
The folder structure appears as in the image below. There are two other files “HelloWorldController.java” and “Message.java”.
Run the application again. The output console window will show as below
Open a browser. In this example, we use the Google Chrome browser. Type http://localhost:8080/HelloWorld, The browser will now display json message “Welcome to HelloWorld!” in the browser.
When the application starts, it starts tomcat and listens for request in port 8080. When we call the url in the browser, it connects to the tomcat server with port 8080 and calls the method “firstPage()” in HelloWorldController.java. This method returns with Message Object. This Message Object is converted to JSON and displayed in the browser.
In this page, we explained how to start with Hello world Rest program in Spring Boot using Spring Tool Suite.