教程:创建您的第一个 Spring 应用程序
本教程介绍如何在 IntelliJ IDEA 中创建和运行 Spring 应用程序。 这是一个由 Spring Boot Maven 项目,由 Spring Initializr生成。 这是创建 Spring 应用程序的最快方式,IntelliJ IDEA 提供了专门的项目向导。 您将学习如何公开一个 HTTP 端点并将其映射到一个方法,当通过网络浏览器访问该方法时,它会向用户返回问候语。
创建一个新的 Spring Boot 项目
在主菜单中,前往 。
在 新建项目 向导的左侧窗格中,选择 Spring Boot。
请为项目指定一个名称:
spring-boot-tutorial。从 JDK 列表中选择 下载JDK 并下载最新版本的 Oracle OpenJDK。
选择最新的 Java 版本。

点击 下一步(N) 继续。
选择 Spring Web 依赖项在 Web 下。 任何使用 Spring MVC 的 web 应用程序都需要此依赖项。

点击 创建 生成并设置项目。
添加一个发送问候的方法
Spring Initializr 创建一个包含 main() 方法的类,以引导您的 Spring 应用程序。 在本教程中,我们将直接向这个类添加 sayHello() 方法。
请打开 SpringBootTutorialApplication.java 文件位于 src/main/java/com/example/springboottutorial 下。
IntelliJ IDEA 提供 转到文件… 操作来快速查找和打开文件。 请前往 或按 Ctrl+Shift+N ,开始键入文件名并从列表中选择。

添加
sayHello()方法,并包含所有必要的注解和导入,使文件看起来像这样:package com.example.springboottutorial; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @RestController public class SpringBootTutorialApplication { public static void main(String[] args) { SpringApplication.run(SpringBootTutorialApplication.class, args); } @GetMapping("/hello") public String sayHello(@RequestParam(value = "myName", defaultValue = "World") String name) { return String.format("Hello %s!", name); } }sayHello()方法接受name参数,并返回与参数值组合的词Hello。 所有其他事务都通过添加 Spring 注释来处理:@RestController注解标记SpringBootTutorialApplication类为请求处理程序(一个 REST 控制器)。@GetMapping("/hello")注释将sayHello()方法映射到/hello的 GET 请求。@RequestParam注解将name方法参数映射到myNameWeb 请求参数。 如果您在网络请求中不提供myName参数,它将默认为World。
运行您的 Spring 应用程序
IntelliJ IDEA 会创建一个 Spring Boot 运行配置,您可以用它运行您的新 Spring 应用。
如果选择了运行配置,请按 Shift+F10。
您还可以使用
图标,它位于 SpringBootTutorialApplication.java 文件的装订区域,紧挨着类声明或
main()方法声明。
默认情况下,IntelliJ IDEA 会在 运行工具窗口中显示您正在运行的 Spring Boot 应用程序。

控制台 选项卡显示 Spring 日志消息的输出。 默认情况下,内置的 Apache Tomcat 服务器正在监听 8080 端口。 请打开您的网页浏览器并前往 http://localhost:8080/hello。 如果您做的一切都正确,您应该会看到您的应用程序响应 Hello World!。

这是默认的通用响应。 您可以在网络请求中提供一个参数,以便应用程序正确地向您问候。 例如,尝试 http://localhost:8080/hello?myName=Human。
添加主页
创建的 Spring Boot 应用程序在 /hello 上有一个可用的端点。 但是,如果您在 http://localhost:8080/ 打开应用程序的根上下文,您将会遇到错误,因为没有定义根资源。 让我们添加一个包含指向您 endpoint 的链接的静态 HTML 主页。
请在 index.html 文件夹下创建 /src/main/resources/static/ 文件。
在 项目 工具窗口中,右键点击 /src/main/resources/static/ 目录,选择 新建 | HTML 文件 ,指定名称 index.html ,然后按 输入 。
请修改默认模板或将其替换为以下 HTML 代码:
<!DOCTYPE HTML> <html> <head> <title>Your first Spring application</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> <p><a href="/hello">Greet the world!</a></p> <form action="/hello" method="GET" id="nameForm"> <div> <label for="nameField">How should the app call you?</label> <input name="myName" id="nameField"> <button>Greet me!</button> </div> </form> </body> </html>在 运行 工具窗口中,点击
或按 Shift+F10 重新启动您的 Spring 应用程序。
现在,您的应用将 index.html 作为根资源提供服务,位于 http://localhost:8080/。
未来计划?
这个简单的应用程序演示了如何开始使用 Spring。 要了解 IntelliJ IDEA 如何帮助您编写代码并在运行时管理应用程序,请参阅 下一个教程 ,其中重点介绍更高级的 Spring 支持功能。