spring入门指南
步骤一:新建一个springboot项目
Use start.spring.io to create a “web” project. In the “Dependencies” section search for and add the “web” dependency as shown in the screenshot. Hit the green “Generate” button, download the zip, and unpack it into a folder on your computer.
使用start.spring.io创建一个web项目。在Dependencies部分搜索并添加web依赖项,如屏幕快照所示,点击绿色“Generate”按钮,下载zip文件,并将其解压缩到您的计算机上的一个文件夹中。
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项目最快、最流行的方式。
添加代码
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中打开项目,并在src/main/java/com/example/demo文件夹中找到DemoApplication.java文件。现在通过添加额外的方法和注解来改变文件的内容,如下面的代码所示。您可以复制并粘贴代码,或者是通过键盘逐行输入代码。
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);
}
}
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”.
我们添加的hello()
方法被设计成接收一个名为name
的字符串参数,然后将该参数与代码中的单词hello
组合起来。这意味着如果您在请求中将name
参数设置为Amy,那么响应将是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.
@RestController
注解告诉Spring,该代码描述了一个端点,该端点应该可以在Web上使用。 @GetMapping(“/hello”)
告诉Spring使用我们的hello()
方法来响应http://localhost:8080/hello
请求。 最后,@RequestParam
告诉Spring该请求中将期待name字段的值,但如果请求中不存在,则name字段将默认使用“World”。
试一试
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:
让我们构建并运行该程序。打开命令行(或终端)并到该项目文件所在的文件夹。我们可以在命令行输入以下命令来构建和运行应用程序
./mvnw spring-boot:run
网友评论