- How to create a Spring Boot Project
- Browse to https://start.spring.io/
- 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
- In the Project dependencies section, check 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.hello.HelloSpringBootApplication
($REPOS_HOME/hello-spring-boot/src/main/java/io/pivotal/hello/HelloSpringBootApplication.java
). You will need to add the import fororg.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.hello.HelloSpringBootApplication
. You will need to add the import fororg.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!";
}
}
- 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()] i.p.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.