Spring Boot Hello World Application
Let’s write a Hello World application with Spring Boot. So let’s go step by step.
First we need to setup the project and create following pom.xml for maven dependencies:
<?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>
<groupId>com.springboot</groupId>
<artifactId>example</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.4.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
[addToAppearHere]
After this we need to create Application class which will run the spring boot application:
package com.springboot.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan({"com.springboot.example"})
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
So let’s create the Controller with the get method which will render “hello world” string as a text/plain.
package com.springboot.example;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloWorldController {
@RequestMapping(value = "/",
method = RequestMethod.GET)
public ResponseEntity helloWorld() {
return ResponseEntity.ok("Hello World");
}
}
So it was really simple, to run the application you need to open in the browser following url:
http://localhost:8080/
In the response you have to see following:
Hello World
You can download the source codes from the github.