已发布·持续改进
Spring Boot 指南 · 1/6
本章目前仅提供英文版。
This chapter walks you from an empty folder to a running Spring Boot application that answers its first HTTP request. Along the way you will set up your editor and add DevTools so the app restarts whenever you change code.
Spring Boot applications run on the JVM, so you need a JDK. The current generation of Spring Boot requires Java 17 or later, and the latest LTS release is a good default. You do not need to install Gradle or Maven: every generated project ships with a wrapper script (gradlew or mvnw) that downloads and runs the right build tool version.
# Check that a JDK is installed
java -versionNew projects usually start at Spring Initializr. Pick a build tool (Gradle or Maven), a language (Java, Kotlin or Groovy), the Spring Boot version, group and artifact names, the Java version and your dependencies, then click Generate to download a zip file. You can do the same from the command line with curl:
curl https://start.spring.io/starter.zip \
-d type=gradle-project \
-d dependencies=web,devtools \
-d baseDir=demo \
-o demo.zip
unzip demo.zip && cd demoThe web dependency is the web starter: Spring MVC plus an embedded Tomcat server. Use type=maven-project for Maven, or add language=kotlin for Kotlin. Run curl https://start.spring.io to list every supported parameter. IntelliJ IDEA, VS Code and Eclipse all call the same Initializr service from their new-project wizards.
The generated DemoApplication.java is the entry point of the application. Add a controller class next to it, in the same package:
package com.example.demo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello(@RequestParam(defaultValue = "World") String name) {
return "Hello, " + name + "!";
}
}@RestController marks a class that handles HTTP requests and writes each method's return value straight into the response body. @GetMapping("/hello") routes GET /hello requests to the method.
Start the application with the command for your build tool. The embedded web server listens on port 8080 by default.
# Gradle project (on Windows: gradlew.bat bootRun)
./gradlew bootRun
# Maven project (on Windows: mvnw.cmd spring-boot:run)
./mvnw spring-boot:run
# In another terminal
curl "http://localhost:8080/hello?name=Spring"If you see Hello, Spring!, everything works. To change the port, add server.port=9090 to src/main/resources/application.properties.
spring-boot-devtools restarts the application whenever files on the classpath change, and applies development-friendly defaults. If you included devtools in the curl command above, it is already there. Otherwise add it like this:
// build.gradle.kts
dependencies {
developmentOnly("org.springframework.boot:spring-boot-devtools")
}<!-- pom.xml -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>DevTools switches itself off when the app runs as a packaged jar with java -jar. In IntelliJ IDEA a restart only happens after the project is rebuilt, so enabling automatic builds makes the loop much smoother.
All of them offer completion and inline documentation for keys in application.properties, so you never have to memorize configuration names.
curl../gradlew bootRun or ./mvnw spring-boot:run.
0 条评论
登录 · 登录后即可发表评论。
来发表第一条评论吧。