Spring boot快速入门三步学习

准备工作

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

第一步

我使用的是IDEA
1.新建工程
在这里插入图片描述
在这里插入图片描述

选择web依赖
在这里插入图片描述
在这里插入图片描述

第二步

新工程目录结构
在这里插入图片描述
1.设置maven
在这里插入图片描述
注意:
红色的,选择自己的maven目录
蓝色的,是自己加载的maven本地仓库
绿色的,不要改。

然后,关闭IDEA,再重新打开
在这里插入图片描述
下载完maven依赖后
在这里插入图片描述
编写控制类HelloController
在这里插入图片描述

package com.example.demo;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
public class HelloController {

    @ResponseBody
    @RequestMapping("/")
    public String hello () {
        return "hello spring boot!";
    }
}

第三步

运行DemoApplication
在这里插入图片描述
会得到控制台以下信息
在这里插入图片描述
打开浏览器,输入http://localhost:8080/
在这里插入图片描述

**

Spring boot成功启动

**
Spring boot快速入门官网,https://spring.io/quickstart
官网译文:
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)
热门选择包括 IntelliJ IDEA,Spring Tools, Visual Studio Code, 或者Eclipse, 还有很多。
Java™开发套件(JDK)
我们推荐 采用OpenJDK 版本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”项目。在“Dependencies”对话框中,搜索并添加“web”依赖关系,如屏幕截图所示。点击“Generate”按钮,下载压缩文件,并将其解压缩到计算机上的文件夹中。

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
步骤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夹中找到文件。现在,通过添加以下代码中所示的额外方法和注释来更改文件的内容。您可以复制并粘贴代码,也可以只键入代码。
在这里插入图片描述
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()采用名为的String参数name,然后将此参数与"Hello"代码中的单词组合。这意味着,如果您“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值,但是如果不存在,默认情况下它将使用单词“ 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:
让我们构建并运行该程序。打开命令行(或终端),然后导航到您拥有项目文件的文件夹。我们可以通过发出以下命令来构建和运行该应用程序:

在这里插入图片描述
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服务器,并监听本地主机端口8080上的请求。打开浏览器,然后在顶部的地址栏中输入http:// localhost:8080/hello。您应该得到一个很好的友好响应,如下所示:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值