Bright Java Tutorial

is brought to you by DoITBright LLC

Spring Boot

Hello World Example

This lesson will show us a very simple example on how to create our first web application using Spring Boot. Let us leave the other theoretical and technical details for now.

To start our development, we will need to have Java Development Kit and Eclipse IDE on our local machine. Extract your Eclipse IDE and place it under the directory c:\>SpringBootStuff. I am using Windows OS and my directory looks like this...

Eclipse on your local directory


Look for the eclipse.exe file inside the 'eclipse' folder. Launch your Eclipse IDE.

Launch Eclipse IDE


Click on the tab 'File', then 'New', then 'Maven Project'. Make sure to select the checkboxes for 'Create a simple project' and 'Use default workspace location'. Click the 'Next' button. Fill out the form as shown...

Create Maven Project


Click the 'Finish' button. Your Maven project named as 'HelloWorldSpringBoot' is created. Here is how it will look like...

Directory file structure of Spring Boot application


Open the 'pom.xml' file and copy and paste the code as shown below...

  <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.brightjavatutorial.spring.boot
    </groupId>
    <artifactId>
      HelloWorldSpringBoot
    </artifactId>
    <version>
      0.0.1-SNAPSHOT
    </version>

    <parent>
      <groupId>
        org.springframework.boot
      </groupId>
      <artifactId>
        spring-boot-starter-parent
      </artifactId>
      <version>
        1.3.5.RELEASE
      </version>
    </parent>

    <dependencies>
      <dependency>
        <groupId>
          org.springframework.boot
        </groupId>
        <artifactId>
          spring-boot-starter-web
        </artifactId>
      </dependency>
    </dependencies>

    <properties>
      <java.version>1.8</java.version>
    </properties>

    <build>
      <plugins>
        <plugin>
          <groupId>
            org.springframework.boot
          </groupId>
          <artifactId>
            spring-boot-maven-plugin
          </artifactId>
        </plugin>
      </plugins>
    </build>
  </project>
                           


Create a Java file under the package 'hello'. Make sure that this package will be in 'C:\SpringBootStuff\HelloWorldSpringBoot\src\main\java\' directory. Name it as 'HelloWorldController.java'.

  package hello;

  import org.springframework.boot.SpringApplication;
  import 
    org.springframework.boot.autoconfigure
      .EnableAutoConfiguration;
  import org.springframework.stereotype.Controller;
  import 
    org.springframework.web.bind.annotation.RequestMapping;
  import 
    org.springframework.web.bind.annotation.ResponseBody;

  import hello.util.SampleUtil;

  @Controller
  @EnableAutoConfiguration
  public class HelloWorldController {

    @RequestMapping("/")
    @ResponseBody
    String home() {
      return "Hello World!";
    }

    @RequestMapping("/myspringboot")
    @ResponseBody
    String myspringboot() {
      SampleUtil util = new SampleUtil();
      return util.determineAddOrEven(3);
    }

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


Create another Java file under the 'hello.util' package.

  package hello.util;

  public class SampleUtil {

    public String determineAddOrEven(int anInt) {
      if(anInt % 2 == 0) {
        return "The number is even.";
      } else {
        return "The number is odd.";
      }
    }
  }
                            


You now have a web application that can be accessed through the URLs 'http://localhost:8080' and 'http://localhost:8080/myspringboot'. I know that there is better way in taking care of the life cycle of the SampleUtil by using Spring IOC but let us leave that for later. The important thing is, we now have a simple application that we can build things on top of it. Let us now run our application. Click on the Maven project 'HelloWorldSpringBoot'. Click on the tab 'Run', then click on 'Run Configurations'. On the left side, look for 'Maven' and click on it. On top, look for 'New Launc Configuration'. It is an icon that looks like a file with a '+' on it. Fill through the form as shown below. Make sure your JVM points to your JDK directory. Click 'Apply' then 'Run'.

Run your Spring Boot application


Try accessing the URL 'http://localhost:8080' and 'http://localhost:8080/myspringboot'. If this is your first time creating and running a Spring Boot application, you will be amazed that you did not have to package your application to a WAR file, and place the WAR file in your '\tomcat\webapps\' directory. Your Spring Boot Hello World application has an Apache Tomcat web application server built in it. This is a feature of Spring Boot.


Next