1. Create A Spring Boot Project

Visit the Spring Initializr at https://start.spring.io/

Spring Initializr

Complete the form as follows:

  1. Generate a Maven Project

  2. In the Project Metadata section, specify io.pivotal for the group id, and hello-spring-boot for the artifact id

  3. In the Dependencies section, add the Web dependency

    The completed form should look something like this:

    Spring Initializr
  4. Click the Generate Project button. Your browser will download a zip file.

  5. Unpack that zip file into a working directory

  6. Import the project’s pom.xml into your editor/IDE of choice.

STS Import Help
To import a maven project into STS..
  • Launch STS

  • Select File > Import…​

  • Select Maven > Existing Maven Projects

  • On the Import Maven Projects page, browse to your hello-spring-boot directory

2. Add an Endpoint

  1. Add a @RestController annotation to the class io.pivotal.HelloSpringBootApplication

    package io.pivotal;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.web.bind.annotation.RestController;
    
    @SpringBootApplication
    @RestController
    public class HelloSpringBootApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(HelloSpringBootApplication.class, args);
        }
    }
    STS Shortcut Help

    Need help adding an import?

    Use the organize imports command:

    • PC: Ctrl+Shift+O

    • Mac: Cmd+Shift+O

    Not sure how to resolve the problem STS is reporting?

    Try the quick-fix (magic shortcut) command:

    • PC: Ctrl+1

    • Mac: Cmd+1

    Other helpful shortcuts.

  2. Add the following request handler:

    @RequestMapping("/")
    public String hello() {
        return "Hello World!";
    }

    The completed class should look like this:

    package io.pivotal;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    @SpringBootApplication
    @RestController
    public class HelloSpringBootApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(HelloSpringBootApplication.class, args);
        }
    
        @RequestMapping("/")
        public String hello() {
            return "Hello World!";
        }
    }

3. Run the hello-spring-boot Application

  1. Open a terminal window and change to hello-spring-boot directory:

    cd hello-spring-boot
  2. Run the application

    mvn spring-boot:run

    You should see the application start up an embedded Apache Tomcat server on port 8080 (review terminal output):

    2015-10-02 13:26:59.264  INFO 44749 --- [lication.main()] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
    2015-10-02 13:26:59.267  INFO 44749 --- [lication.main()] io.pivotal.hello.HelloSpringBootApplication     : Started HelloSpringBootApplication in 2.541 seconds (JVM running for 9.141)
  3. Browse to http://localhost:8080/

    Hello World

Stop the hello-spring-boot application (press Ctrl+C in the terminal window).


Congratulations! You’ve just written your first Spring Boot application.