Spring Boot 简明教程

Spring Boot - Quick Start

本章将教你如何使用 Maven 和 Gradle 创建 Spring Boot 应用程序。

This chapter will teach you how to create a Spring Boot application using Maven and Gradle.

Prerequisites

你的系统需要满足以下最低要求才能创建 Spring Boot 应用程序:

Your system need to have the following minimum requirements to create a Spring Boot application −

  1. Java 7

  2. Maven 3.2

  3. Gradle 2.5

Spring Boot CLI

Spring Boot CLI 是一款命令行工具,它允许我们运行 Groovy 脚本。这是使用 Spring Boot 命令行界面创建 Spring Boot 应用程序最简单的方法。你可以在命令提示符中创建、运行和测试应用程序。

The Spring Boot CLI is a command line tool and it allows us to run the Groovy scripts. This is the easiest way to create a Spring Boot application by using the Spring Boot Command Line Interface. You can create, run and test the application in command prompt itself.

本节向你介绍手动安装 Spring Boot CLI 所涉及的步骤。如需获得进一步的帮助,你可以使用以下链接: https://docs.spring.io/springboot/ docs/current-SNAPSHOT/reference/htmlsingle/#getting-started-installing-springboot

This section explains you the steps involved in manual installation of Spring Boot CLI. For further help, you can use the following link: https://docs.spring.io/springboot/ docs/current-SNAPSHOT/reference/htmlsingle/#getting-started-installing-springboot

You can also download the Spring CLI distribution from the Spring Software repository at: https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#getting-started-manual-cli-installation

对于手动安装,你需要使用以下两个文件夹:

For manual installation, you need to use the following two folders −

  1. spring-boot-cli-2.0.0.BUILD-SNAPSHOT-bin.zip

  2. spring-boot-cli-2.0.0.BUILD-SNAPSHOT-bin.tar.gz

下载后,解压存档文件并按照 install.txt 文件中给出的步骤操作。不,它不需要任何环境设置。

After the download, unpack the archive file and follow the steps given in the install.txt file. Not that it does not require any environment setup.

在 Windows 中,在命令提示符中进入 Spring Boot CLI bin 目录,然后运行命令 spring –-version 以确保 spring CLI 正确安装。执行该命令后,您可以看到 spring CLI 版本,如下所示 −

In Windows, go to the Spring Boot CLI bin directory in the command prompt and run the command spring –-version to make sure spring CLI is installed correctly. After executing the command, you can see the spring CLI version as shown below −

spring cli version

Run Hello World with Groovy

创建一个简单的 groovy 文件,其中包含 Rest Endpoint 脚本,并使用 spring boot CLI 运行该 groovy 文件。观察此处为此目的所示的代码 −

Create a simple groovy file which contains the Rest Endpoint script and run the groovy file with spring boot CLI. Observe the code shown here for this purpose −

@Controller
class Example {
   @RequestMapping("/")
   @ResponseBody
   public String hello() {
      "Hello Spring Boot"
   }
}

现在,以 hello.groovy 的名称保存该 groovy 文件。请注意在此示例中,我们将其 groovy 文件保存在 Spring Boot CLI bin 目录中。现在,使用命令 spring run hello.groovy 运行应用程序,如下所示屏幕截图 −

Now, save the groovy file with the name hello.groovy. Note that in this example, we saved the groovy file inside the Spring Boot CLI bin directory. Now run the application by using the command spring run hello.groovy as shown in the screenshot given below −

run hello world with groovy

运行该 groovy 文件后,所需依赖项将自动下载,它将在 Tomcat 8080 端口启动应用程序,如下所示屏幕截图 −

Once you run the groovy file, required dependencies will download automatically and it will start the application in Tomcat 8080 port as shown in the screenshot given below −

run groovy file tomcat port

一旦 Tomcat 启动,转到 web 浏览器并点击该 URL http://localhost:8080/ ,您可以看到所示的输出。

Once Tomcat starts, go to the web browser and hit the URL http://localhost:8080/ and you can see the output as shown.

hello spring boot