提交 954b832a 编写于 作者: M Mao

translate quickstart

上级 b43b177b
# Spring Quickstart Guide
## What you'll build
You will build a classic “Hello World!” endpoint which any browser can connect to. You can even tell it your name, and it will respond in a more friendly way.
## What you’ll need
**An Integrated Developer Environment (IDE)**
Popular choices include [IntelliJ IDEA](https://www.jetbrains.com/idea/), [Spring Tools](https://spring.io/tools), [Visual Studio Code](https://code.visualstudio.com/docs/languages/java), or [Eclipse](https://www.eclipse.org/downloads/packages/), and many more.
**A Java™ Development Kit (JDK)**
We recommend [BellSoft Liberica JDK](https://bell-sw.com/) version 8 or version 11.
## Step 1: Start a new Spring Boot project
Use [start.spring.io](https://start.spring.io/) to create a “web” project. In the “Dependencies” dialog search for and add the “web” dependency as shown in the screenshot. Hit the “Generate” button, download the zip, and unpack it into a folder on your computer.
![quick-img-1-12bfde9c5c280b1940d85dee3d81772d](./quickstart_img/quick-img-1-12bfde9c5c280b1940d85dee3d81772d.png)
Projects created by [start.spring.io](https://start.spring.io/) contain [Spring Boot](https://spring.io/projects/spring-boot), a framework that makes Spring ready to work inside your app, but without much code or configuration required. Spring Boot is the quickest and most popular way to start Spring projects.
## Step 2: Add your code
Open up the project in your IDE and locate the `DemoApplication.java` file in the `src/main/java/com/example/demo` folder. Now change the contents of the file by adding the extra method and annotations shown in the code below. You can copy and paste the code or just type it.
```
package com.example.demo;
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 DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@GetMapping("/hello")
public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {
return String.format("Hello %s!", name);
}
}
```
*This is all the code required to create a simple “Hello World” web service in Spring Boot.*
The `hello()` method we’ve added is designed to take a String parameter called `name`, and then combine this parameter with the word `"Hello"` in the code. This means that if you set your name to `“Amy”` in the request, the response would be `“Hello Amy”`.
The `@RestController` annotation tells Spring that this code describes an endpoint that should be made available over the web. The `@GetMapping(“/hello”)` tells Spring to use our `hello()` method to answer requests that get sent to the `http://localhost:8080/hello` address. Finally, the `@RequestParam` is telling Spring to expect a `name` value in the request, but if it’s not there, it will use the word “World” by default.
## Step 3: Try it
Let’s build and run the program. Open a command line (or terminal) and navigate to the folder where you have the project files. We can build and run the application by issuing the following command:
**MacOS/Linux:**
```
./mvnw spring-boot:run
```
**Windows:**
```
mvnw spring-boot:run
```
You should see some output that looks very similar to this:
![quick-img2-ac5ae88c60ffaa062234a580f9f1abc3](./quickstart_img/quick-img2-ac5ae88c60ffaa062234a580f9f1abc3.png)
The last couple of lines here tell us that Spring has started. Spring Boot’s embedded Apache Tomcat server is acting as a webserver and is listening for requests on `localhost` port `8080`. Open your browser and in the address bar at the top enter http://localhost:8080/hello. You should get a nice friendly response like this:
![quick-img3-afa0a1fe446db8e3c8c7a8d9ca532d23](./quickstart_img/quick-img3-afa0a1fe446db8e3c8c7a8d9ca532d23.png)
\ No newline at end of file
# Spring 快速入门指南
## 您将构建什么
您将构建一个经典的“Hello World!” 任何浏览器都可以连接的端点。你甚至可以告诉它你的名字,它会以更友好的方式回应。
## 你需要什么
**集成开发人员环境 (IDE)**
热门选择包括[IntelliJ IDEA](https://www.jetbrains.com/idea/),[弹簧工具](https://spring.io/tools),[视觉工作室代码](https://code.visualstudio.com/docs/languages/java), 要么[](https://www.eclipse.org/downloads/packages/), 还有很多。
**Java™ 开发工具包 (JDK)**
我们推荐[BellSoft Liberica JDK](https://bell-sw.com/)版本 8 或版本 11。
## 第一步:启动一个新的 Spring Boot 项目
采用[启动.spring.io](https://start.spring.io/)创建一个“网络”项目。在“依赖项”对话框中搜索并添加“web”依赖项,如屏幕截图所示。点击“生成”按钮,下载 zip,然后将其解压缩到计算机上的文件夹中。
![quick-img-1-12bfde9c5c280b1940d85dee3d81772d](./quickstart_img/quick-img-1-12bfde9c5c280b1940d85dee3d81772d.png)
创建的项目[启动.spring.io](https://start.spring.io/)包含[弹簧靴](https://spring.io/projects/spring-boot),一个使 Spring 准备好在您的应用程序中工作的框架,但不需要太多代码或配置。Spring Boot 是启动 Spring 项目的最快和最流行的方式。
## 第 2 步:添加您的代码
在 IDE 中打开项目并在文件夹`DemoApplication.java`中找到该文件`src/main/java/com/example/demo`。现在通过添加下面代码中显示的额外方法和注释来更改文件的内容。您可以复制并粘贴代码或直接输入。
```
package com.example.demo;
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 DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@GetMapping("/hello")
public String hello(@RequestParam(value = "name", defaultValue = "World") String name) {
return String.format("Hello %s!", name);
}
}
```
这是在 Spring Boot 中创建一个简单的“Hello World”Web 服务所需的所有代码。
`hello()`我们添加的方法旨在获取一个名为 的字符串参数`name`,然后将该参数与`"Hello"`代码中的单词结合起来。这意味着如果您`“Amy”`在请求中将您的姓名设置为,则响应将为`“Hello Amy”`.
`@RestController`注释告诉 Spring 这段代码描述了一个应该在 web 上可用的端点。`@GetMapping(“/hello”)`告诉 Spring 使用我们的方法`hello()`来回答发送到该`http://localhost:8080/hello`地址的请求。最后,`@RequestParam`告诉 Spring`name`在请求中期待一个值,但如果它不存在,它将默认使用单词“World”。
## 第 3 步:尝试一下
让我们构建并运行程序。打开命令行(或终端)并导航到您拥有项目文件的文件夹。我们可以通过发出以下命令来构建和运行应用程序:
**MacOS/Linux:**
```
./mvnw spring-boot:run
```
**Windows:**
```
mvnw spring-boot:run
```
您应该会看到一些与此非常相似的输出:
![quick-img2-ac5ae88c60ffaa062234a580f9f1abc3](./quickstart_img/quick-img2-ac5ae88c60ffaa062234a580f9f1abc3.png)
这里的最后几行告诉我们春天已经开始了。Spring Boot 的嵌入式 Apache Tomcat 服务器充当 Web 服务器,并正在侦听`localhost`port上的请求`8080`。打开浏览器,在顶部的地址栏中输入[http://localhost:8080/你好](http://localhost:8080/hello). 你应该得到一个很好的友好回应,如下所示:
![quick-img3-afa0a1fe446db8e3c8c7a8d9ca532d23](./quickstart_img/quick-img3-afa0a1fe446db8e3c8c7a8d9ca532d23.png)
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册