Springboot三步快速入门

开始准备工作

  1. 需要集成开发人员环境
    例如: IntelliJ IDEA、Spring Tools、Visual Studio Code、Eclipse等
  2. 需要jdk 8 或者jdk 11
  3. 需要下载并配置maven依赖

第一步

使用软件为IntelliJ IDEA

  1. 新建工程
    Springboot三步快速入门
  2. 选择web依赖
    Springboot三步快速入门

第二步

  1. 新工程目录结构
    Springboot三步快速入门
  2. 设置maven
    Springboot三步快速入门

maven home directory是选择自己的maven安装路径
User settings file选择你安装路径下conf里的setting文件
Local repository 选择加载你的maven本地仓库

  1. 然后重启IDEA
  2. 下载完maven依赖后编写控制类HelloControllerSpringboot三步快速入门

第三步

  1. 运行DemoApplication
  2. 当控制台出现Spring时运行成功
    Springboot三步快速入门
  3. 在浏览器中输入http://localhost:8080/
    Springboot三步快速入门
  4. 验证成功!!!

Spring boot成功启动

Spring boot快速入门官网,https://spring.io/quickstart
官网译文:
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.

翻译:

你会建立什么
您将构建一个经典的“ Hello World!”。任何浏览器都可以连接到的端点。您甚至可以告诉它您的名字,它将以更友好的方式响应。

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.

翻译:

你需要什么
集成开发环境(IDE)
受欢迎的选择包括再观, 弹簧工具, VisualStudio代码,或月食还有更多。
JAVA™开发工具包(JDK)
我们建议AdoptOpenJDK第8版或第11版。

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”依赖项,如屏幕快照所示。点击“Generate”按钮,下载zip,然后将其解压到计算机上的一个文件夹中。
Springboot三步快速入门
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可以在应用程序中工作,但不需要太多的代码或配置。SpringBoot是启动Spring项目最快也是最受欢迎的方式。

Step 2: Add your code
步骤2:添加您的代码

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文件夹。现在,通过添加下面代码中显示的额外方法和注释来更改文件的内容。您可以复制和粘贴代码,或者只需键入它。Springboot三步快速入门
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()方法被设计为接受一个名为name,然后将此参数与Word组合。"Hello"在密码里。这意味着如果你把你的名字“Amy”在请求中,答复将是“Hello Amy”.

这个@RestController注释告诉Spring,这段代码描述了应该通过Web提供的端点。这个@GetMapping(“/hello”)告诉Spring使用我们的hello()方法来响应发送到http://localhost:8080/hello地址。最后,@RequestParam告诉Springname值,但如果不存在,则默认使用“World”一词。

Step 3: Try it
步骤3:尝试

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:Springboot三步快速入门
Windows:
Springboot三步快速入门
You should see some output that looks very similar to this:

翻译:

您应该会看到一些非常类似于此的输出:Springboot三步快速入门
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 Boot的嵌入式ApacheTomcat服务器充当Web服务器,并侦听localhost港8080。打开您的浏览器,并在地址栏的顶部回车。Http://localhost:8080/hello。你应该得到这样的友好回应:Springboot三步快速入门