Estimated time: 25 minutes
- How to create a Spring Boot Project
- Browse to https://start.spring.io/
Fill Out the Form Accordingly:
-
Generate a
Maven Project
with Spring Boot1.2.6
. -
In the Project Metadata section, add the following:
Group
io.pivotal
Artifact
hello-spring-boot
- In the Dependencies section, add the following:
Web
-
Click the Generate Project button. Your browser will download a zip file. Unpack that zip file into the repos directory ($REPOS_HOME).
-
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 a
@RestController
annotation to the classio.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.
- 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!";
}
}
- Open a terminal window and change to
hello-spring-boot
directory:
$ cd $REPOS_HOME/hello-spring-boot
- Run the application
mvn clean 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)
- Browse to: http://localhost:8080/
- Stop the
hello-spring-boot
application. In the terminal window:Ctrl + C
Congratulations! You’ve just completed your first Spring Boot application.