Spring快速入门指南网站翻译

Step 1: Start a new Spring Boot project(开始一个新的Spring Boot项目)

Use 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.

(用 start.spring.io创建一个“web”项目。在“依赖关系”对话框中,搜索并添加“web”依赖关系,如屏幕截图所示。点击“生成”,下载压缩文件,并将其解压安装在你的电脑上。)

在这里插入图片描述
Projects created by start.spring.io contain 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.

(创建的项目 start.spring.io 包含Spring Boot ,这是一个框架,可以使Spring准备在您的应用程序内部工作,而无需太多代码或配置。Spring Boot是启动Spring项目的最快,最受欢迎的方式。)

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.

(在IDE中打开项目,然后在DemoApplication.java文件src/main/java/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);
   }
}

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.

(这个hello()是我们添加的方法指在采用名为的String参数name,然后将此参数与"Hello"代码中的单词组合。这意味着,如果您“Amy”在请求中将姓名设置,则会相应为“Hello Amy”。@RestController注释告诉Spring,这个代码的描述应该可在网上找到端点。@GetMapping(“/hello”)告诉Spring使用我们的hello()方法来回答这个问题被发送到请求http://localhost:8080/hello的地址。@RequestParam告诉Spring name在请求中期望一个值,但是如果不存在,默认情況下它将使用单词“ World”。)

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:
(您应该能看到一些与此图非常相似的输出:)
在这里插入图片描述
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:

(最后两行告诉我们Spring已经开始运行。Spring Boot的嵌入式Apache Tomcat服务器充当Web服务器,并在localhostport 上监听到请求8080。打开浏览器,然后在顶部的地址栏中輸入http:// localhost:8080 /hello。您应该得到一个很好的友好相应,如下所示:)

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值