Spring快速指南(Spring Quickstart Guide)
Spring Quickstart Guide
Spring快速指南
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, Spring Tools, Visual Studio Code, or Eclipse, and many more.
A Java™ Development Kit (JDK)
We recommend AdoptOpenJDK version 8 or version 11.
你会构建什么?
你会构建一个"Hello World"类, 最终任何浏览器都可以连接他. 你甚至可以告诉他你的名字, 然后他会以一个更友好的方式回应你.
你需要什么?
一个完整的开发环境(IDE)
流行中的选择是InteliJ IDEA, Spring Tools, Visual Studio Code还有Eclipse以及更多.
一个Java开发者套件(JDK)
我们推荐AdoptOpenJDK 8
Step 1: Start a new Spring Boot project
步骤1: 开始一个新的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"依赖.点击"构建"按钮, 下载zip文件并在你的计算机中解压为一个文件夹.
https://anying-picture-universal-public-read.oss-cn-beijing.aliyuncs.com/notes-images/quick-img-1-12bfde9c5c280b1940d85dee3d81772d.pngProjects 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准备好迎接的你的app了, 但是没有更多必须的代码和配置.Spring Boot 是启动Spring项目最快, 最流行的方式.
Step 2: Add your code
步骤2: 添加你的代码
Open up the project in your IDE and locate the
DemoApplication.javafile in thesrc/main/java/com/example/demofolder. 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// '@'开头的称为注解(annotation)
@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 calledname, 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
@RestControllerannotation tells Spring that this code describes an endpoint that should be made available over the web. The@GetMapping(“/hello”)tells Spring to use ourhello()method to answer requests that get sent to thehttp://localhost:8080/helloaddress. Finally, the@RequestParamis telling Spring to expect anamevalue in the request, but if it’s not there, it will use the word “World” by default.
我们添加的hello()方法被设计为接受一个名为name的字符串参数, 然后会在代码中联合单词Hello. 这就说明如果你在请求中设置你的名字为"Amy", 那么恢复就应该是Hello Amy.
注解@RestController告诉Spring这些代码描述了一个在web中可用的端点. 注解@GetMapping("/hello"告诉Spring如果被请求http://localhost:8080/hello地址, 就应该使用hello()方法去回应请求. 最终@RequestParm告诉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:
让我们构建和运行程序. 打开命令行终端(或terminal)然后导航到有你的项目文件的文件夹. 我们发出以下命令以运行构建和运行应用.
MacOS/Linux:
./mvnw spring-boot:run
Windows:
mvnw spring-boot:run
You should see some output that looks very similar to this:
你应该看到一些这样非常少的输出.
https://anying-picture-universal-public-read.oss-cn-beijing.aliyuncs.com/notes-images/quick-img2-ac5ae88c60ffaa062234a580f9f1abc3.pngThe 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
localhostport8080. 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 WEB服务器由内置Apache Toccat 服务器代理并监听到localhost端口8080. 打开你的浏览器然后键入地址http://localhost:8080/hello并进入. 你可以得到非常友好回应, 就像这样:
https://anying-picture-universal-public-read.oss-cn-beijing.aliyuncs.com/notes-images/quick-img3-afa0a1fe446db8e3c8c7a8d9ca532d23.png#### Pop quiz
突击测验
What should happen if you add
?name=Amyto the end of the URL?
如果你添加?name=Amy到URL的最后会发生什么?
Next, try these popular guides
下一步, 尝试这些受欢迎的指南
You’ve already seen how simple Spring can be, but it’s also very flexible. There are thousands of things you can do with Spring, and we have lots of guides available to take you through the most popular choices. Why not keep on learning and try one of these additional guides?
你可能已经看到了Spring是多么的简单, 但他也是非常灵活的. 你可以在Spring中做上千种事, 而且我们还有很多指南能让你去选. 为什么继续学习这些额外的指南其中之一呢?
* Continue your learning by creating a RESTful JSON web service in Spring
* Learn how to retrieve web page data with Spring's RestTemplate.
* Learn how to work with JPA data persistence using Spring Data JPA.
在spring中创建一个RESTful JSON web以继续你的学习
学习如何在Spring的
Rest模板中检索web页面的数据.学习持续使用Spring Data JPA的JPA是如何工作的.