Spring Boot Hello World Step by Step
Spring Boot Hello World Step by Step: Spring boot is a brand new framework provides opinionated approach for basic configurations and helping developers to directly dig into business logics. Just follow this tutorial, you will understand better after creating the first hello world spring boot application.
Table of Contents
Prerequisite:
Spring STS Eclipse Tool
Video Tutorial:
Step 1: Create spring starter project
New -> Spring Starter Project
Step 2: Select Web and thymeleaf from the Dependencies
Thymeleaf is a view resolver and web is needed to add all the web dependencies.
Step 3: Create HelloController.java
Create HelloController.java in src/main/java folder
@controller: Creates a controller bean in spring container.
@RequestMapping: Maps /hello url to hello() method. When the /hello request comes then hello() method will be called out then it will find out hello-view in templates and through view resolver (here thymeleaf) content of hello-view will be rendered.
If suppose you want to render any xml/json outputs then either you can use @RestController or @Controller and @ResponseBody just below to @RequestMapping.
HelloController.java
[java]
package in.javadomain;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class HelloController {
@RequestMapping(“/hello”)
public String hello(){
return “hello-view”;
}
}
[/java]
Step 4: Create hello-view.html file under templates
Create hello-view.html in src/main/resources/templates folder
hello-view.html
[html]
<!DOCTYPE html>
<html>
<head>
<title>Hello</title>
</head>
<body>
Success!!
</body>
</html>
[/html]
Step 5: Running the application
Right click on the project Run as -> Spring Boot App
Note: Source code download option is not provided in the article, because totally just two files and full source code of the files are given above itself.
Feel free to write your comments suggestions thoughts below. Thanks for reading this post!.