Skip to content

Latest commit

 

History

History
162 lines (101 loc) · 3.84 KB

getting-started.md

File metadata and controls

162 lines (101 loc) · 3.84 KB

Spring Boot

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

  1. Fill out the Project metadata fields as follows:

Group

io.pivotal

Artifact

hello-spring-boot

Name

hello-spring-boot

Description

Hello Spring Boot

Package Name

io.pivotal.hello

Type

Maven Project

Packaging

Jar

Java Version

1.8

Language

Java

Spring Boot Version

1.2.6

  1. In the Project dependencies section, check the following:
  • Web
  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).

  1. Add a @RestController annotation to the class io.pivotal.hello.HelloSpringBootApplication ($REPOS_HOME/hello-spring-boot/src/main/java/io/pivotal/hello/HelloSpringBootApplication.java). You will need to add the import for 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.hello.HelloSpringBootApplication. You will need to add the import for org.springframework.web.bind.annotation.RequestMapping.
@RequestMapping("/")
public String hello() {
    return "Hello World!";
}

Completed:

@SpringBootApplication
@RestController
public class HelloSpringBootApplication {

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

    @RequestMapping("/")
    public String hello() {
        return "Hello World!";
    }
}
  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()] i.p.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.