Spring Boot hello world tutorial

Spring Boot is one of the most powerful Java EE frameworks that aims to facilitate the development of new application. It brings all the power of the Spring stack (Spring dependency injection, Spring data JPA, Spring ORM, Spring MVC…) combined with an embedded Tomcat/Jetty server avoiding you heavy deployment.

One of the strong point of Spring Boot is the automatic configuration. In fact, you have just to respect some naming conventions and Spring Boot will do the job for you all by giving you the flexibility to override the defaults configurations if you want.

This tutorial will present a basic “hello world !” application using Spring Boot.

<!-- Spring test to make unit testing. Includes JUnit, Hamcrest and Mockito -->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-test</artifactId>

<scope>test</scope>

</dependency>

</dependencies>

<build>

<plugins>

<!-- Make the project as an executable jar -->

<plugin>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>

</plugin>

</plugins>

</build>

</project>

Spring Boot main class : Application.java

1

2

3

4

5

6

7

8

9

10

importorg.springframework.boot.SpringApplication;

importorg.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication

publicclassApplication{

publicstaticvoidmain(String[]args){

SpringApplication.run(Application.class,args);

}

}

The
Application.java file would declare the main method that uses
SpringApplication.run() method to launch the application. It’s annotated with
@SpringBootApplication which is equivalent to using
@Configuration,
@EnableAutoConfiguration and
@ComponentScan all together.

Consider to put your Application.java in the root package of your application. Why ? Because Spring Boot implicitly defines its package as the search package to search you spring component (@Component), your JPA entities (@Entity), your class configuration (@Configuration) etc. Like this, you don’t have to specify a @ComponentScan and Spring Boot will do the job for you as you will see in the next section.

Spring component : HelloWorldService.java

Let’s consider the following basic Spring component containing the method
sayHelloWorld() that returns a String.

1

2

3

4

5

6

7

8

9

10

11

12

packagecom.roufid.tutorial.services;

importorg.springframework.stereotype.Component;

@Component

publicclassHelloWorldService{

publicStringsayHelloWorld(){

return"Hello World !";

}

}

The
HelloWorldService is annotated with
@Component in order to tell Spring to register this class as Spring Bean.

Spring MVC Controller : HelloWorldController.java

Below a simple Spring MVC controller containing a method that will be called on the root context of our application. This method calls the
helloWorldService created in the previous section that we inject with the annotation
@Autowired

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

packagecom.roufid.tutorial.controller;

importorg.springframework.beans.factory.annotation.Autowired;

importorg.springframework.stereotype.Controller;

importorg.springframework.web.bind.annotation.RequestMapping;

importorg.springframework.web.bind.annotation.ResponseBody;

importcom.roufid.tutorial.services.HelloWorldService;

@Controller

publicclassHelloWorldController{

@Autowired

privateHelloWorldService helloWorldService;

@RequestMapping("/")

@ResponseBody

publicStringhelloWorld(){

returnhelloWorldService.sayHelloWorld();

}

}

That’s all, the application is ready now to use !

Running the application

Right-Click on your project -> Run As -> Spring Boot App or Right-Click on Application.java -> Run As-> Java Application