Skip to content

Latest commit

 

History

History
160 lines (103 loc) · 4.37 KB

getting-started.md

File metadata and controls

160 lines (103 loc) · 4.37 KB

Spring Boot

Estimated time: 25 minutes

Requirements

Lab Requirements

What You Will Learn

  • How to create a Spring Boot Project

Exercises

Create A Spring Boot Project

  1. Browse to https://start.spring.io/

Spring Initializr

Fill Out the Form Accordingly:

  1. Generate a Maven Project with Spring Boot 1.2.6.

  2. In the Project Metadata section, add the following:

Group

io.pivotal

Artifact

hello-spring-boot

  1. In the Dependencies section, add the following:
  • Web

Completed Form: Spring Initializr

  1. Click the Generate Project button. Your browser will download a zip file. Unpack that zip file into the repos directory ($REPOS_HOME).

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

STS Import Help:

Select File > Import... Then select Maven > Existing Maven Projects. On the Import Maven Projects page, browse to your $REPOS_HOME/hello-spring-boot (e.g. ~/repos/hello-spring-boot).

Add an Endpoint

  1. Add a @RestController annotation to the class io.pivotal.HelloSpringBootApplication ($REPOS_HOME/hello-spring-boot/src/main/java/io/pivotal/HelloSpringBootApplication.java).
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.

  1. Add the following request handler to the class io.pivotal.HelloSpringBootApplication ($REPOS_HOME/hello-spring-boot/src/main/java/io/pivotal/HelloSpringBootApplication.java).
@RequestMapping("/")
public String hello() {
    return "Hello World!";
}

Completed:

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!";
    }
}

Run the hello-spring-boot Application

  1. Open a terminal window and change to hello-spring-boot directory:
$ cd $REPOS_HOME/hello-spring-boot
  1. Run the application
mvn clean spring-boot:run
  1. 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)
  1. Browse to: http://localhost:8080/

Hello World

  1. Stop the hello-spring-boot application. In the terminal window: Ctrl + C

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