date
Jun 25, 2021
type
Post
status
Published
slug
demo-spring-boot
summary
本文给大家展示基于SpringBoot快速搭建Java后端服务,当然Java开发环境还是需要提前准备好的:1 Jdk1.8安装并配置环境变量;2 Maven安装配置环境变量;3 Idea开发工具安装等
tags
spring-boot
category
spring-boot
password
Property
Sep 27, 2022 02:57 AM
icon
一、SpringBoot介绍
![友情链接:Spring Boot_百度百科](https://www.notion.so/image/https%3A%2F%2Ffile.notion.so%2Ff%2Ff%2Faa032398-6ed6-40c4-987e-e8a138ae8ea0%2F7d883f2e-e70c-4249-9c05-28d98c30f7d7%2FUntitled.png%3Fid%3Ddeadc984-7b45-40de-b27c-e7fe168eeb49%26table%3Dblock%26spaceId%3Daa032398-6ed6-40c4-987e-e8a138ae8ea0%26expirationTimestamp%3D1719568800000%26signature%3Dm8G51qU_z5kD34yhMhRoBxSx65qKn0DTJkgUtW_feA4?table=block&id=deadc984-7b45-40de-b27c-e7fe168eeb49&cache=v2)
二、两种方式快速创建SpringBoot项目
方式一:spring官网快速生成
1. 浏览器访问地址:https://start.spring.io/
![notion image](https://www.notion.so/image/https%3A%2F%2Ffile.notion.so%2Ff%2Ff%2Faa032398-6ed6-40c4-987e-e8a138ae8ea0%2F49054440-0684-4c37-9caa-362797bb60eb%2FUntitled.png%3Fid%3Dbacb2e61-7dd8-453b-ab67-5742f9d5d031%26table%3Dblock%26spaceId%3Daa032398-6ed6-40c4-987e-e8a138ae8ea0%26expirationTimestamp%3D1719568800000%26signature%3DwkiGA8ygrflx3HHvtMiVrlJX337M0CqG2B0kapyRzeY?table=block&id=bacb2e61-7dd8-453b-ab67-5742f9d5d031&cache=v2)
2. 导入项目到IntelliJ IDEA
- 解压项目到指定工作空间
- 用Idea打开项目即可导入,菜单file→open,指定项目文件夹
方式二:使用Idea直接快速创建SpringBoot项目
1. 点击菜单 File→ new→ project ,弹出页面
![notion image](https://www.notion.so/image/https%3A%2F%2Ffile.notion.so%2Ff%2Ff%2Faa032398-6ed6-40c4-987e-e8a138ae8ea0%2F55499422-70a3-4963-8f0c-8f20da25d7b5%2FUntitled.png%3Fid%3D158bffcf-6a34-41a9-93a4-10cf012a6aca%26table%3Dblock%26spaceId%3Daa032398-6ed6-40c4-987e-e8a138ae8ea0%26expirationTimestamp%3D1719568800000%26signature%3DiqRaDk2VzgH9KTCSBgnJJ04Uu3Bp-64IcJz-hg3LXQw?table=block&id=158bffcf-6a34-41a9-93a4-10cf012a6aca&cache=v2)
如果访问不了https://start.spring.io/,可以选择自定义地址,并输入国内地址:https://start.aliyun.com来创建springboot项目。
![notion image](https://www.notion.so/image/https%3A%2F%2Ffile.notion.so%2Ff%2Ff%2Faa032398-6ed6-40c4-987e-e8a138ae8ea0%2Ff443e1b0-7fef-4a25-b3bb-d154f2476f66%2FUntitled.png%3Fid%3D7dd0d3fb-edd9-4716-9b92-ee469a9dabd3%26table%3Dblock%26spaceId%3Daa032398-6ed6-40c4-987e-e8a138ae8ea0%26expirationTimestamp%3D1719568800000%26signature%3DGngO-rgul10GG4Q79BTuW8uzjoAYd28bvO83FjjIipw?table=block&id=7dd0d3fb-edd9-4716-9b92-ee469a9dabd3&cache=v2)
2. 修改Java Version
![notion image](https://www.notion.so/image/https%3A%2F%2Ffile.notion.so%2Ff%2Ff%2Faa032398-6ed6-40c4-987e-e8a138ae8ea0%2Fa57826eb-c637-448e-8586-6070b26ec584%2FUntitled.png%3Fid%3Df02d9962-c28d-4693-9802-993a8f5ae433%26table%3Dblock%26spaceId%3Daa032398-6ed6-40c4-987e-e8a138ae8ea0%26expirationTimestamp%3D1719568800000%26signature%3DwOx82OFRus9Iwahhh4OnWbOU-vneOxtJ-8pe6qO9npA?table=block&id=f02d9962-c28d-4693-9802-993a8f5ae433&cache=v2)
3. 添加Spring Web依赖
![notion image](https://www.notion.so/image/https%3A%2F%2Ffile.notion.so%2Ff%2Ff%2Faa032398-6ed6-40c4-987e-e8a138ae8ea0%2F07ae0bee-541a-4109-bdf5-191de60f0f57%2FUntitled.png%3Fid%3Dbb2a4e5a-8d6d-4ef7-ba3d-bb6744d6e23d%26table%3Dblock%26spaceId%3Daa032398-6ed6-40c4-987e-e8a138ae8ea0%26expirationTimestamp%3D1719568800000%26signature%3Dj8Krq02hWyfUF8kHNAGoFA-Z2HC1gCxeYxU0zGXPaw8?table=block&id=bb2a4e5a-8d6d-4ef7-ba3d-bb6744d6e23d&cache=v2)
4. 配置项目名称和路径信息
![notion image](https://www.notion.so/image/https%3A%2F%2Ffile.notion.so%2Ff%2Ff%2Faa032398-6ed6-40c4-987e-e8a138ae8ea0%2Ffbb5e17f-83ac-489b-b261-a540d81d04ad%2FUntitled.png%3Fid%3D6fafe1c8-3e57-4984-a007-1e4ea50b06e7%26table%3Dblock%26spaceId%3Daa032398-6ed6-40c4-987e-e8a138ae8ea0%26expirationTimestamp%3D1719568800000%26signature%3DMyzYNdeDp0avknnrJykQmiGOzCuaCbSlKLcmKxZ4YxM?table=block&id=6fafe1c8-3e57-4984-a007-1e4ea50b06e7&cache=v2)
5. 点击finish成功创建SpringBoot项目
![notion image](https://www.notion.so/image/https%3A%2F%2Ffile.notion.so%2Ff%2Ff%2Faa032398-6ed6-40c4-987e-e8a138ae8ea0%2F482c1681-a465-4f86-9063-97387fd86abd%2FUntitled.png%3Fid%3D296c58cd-c9ef-4977-a5bc-c31769943f08%26table%3Dblock%26spaceId%3Daa032398-6ed6-40c4-987e-e8a138ae8ea0%26expirationTimestamp%3D1719568800000%26signature%3D4yDVSaaZDEccWHmkBdRbjOeUJIXVTeLxYOecU-21T90?table=block&id=296c58cd-c9ef-4977-a5bc-c31769943f08&cache=v2)
6. 有件项目包路径,创建HelloWorldController.java
![notion image](https://www.notion.so/image/https%3A%2F%2Ffile.notion.so%2Ff%2Ff%2Faa032398-6ed6-40c4-987e-e8a138ae8ea0%2F99ce4684-dbf6-4778-9f3a-460ad513c82e%2FUntitled.png%3Fid%3Ded278017-a191-41e8-8a32-2eb1104344cb%26table%3Dblock%26spaceId%3Daa032398-6ed6-40c4-987e-e8a138ae8ea0%26expirationTimestamp%3D1719568800000%26signature%3Drn7zK9GTVlLuOfgaGqOxziZrNU7rzXxVB5zvv3sim2c?table=block&id=ed278017-a191-41e8-8a32-2eb1104344cb&cache=v2)
![notion image](https://www.notion.so/image/https%3A%2F%2Ffile.notion.so%2Ff%2Ff%2Faa032398-6ed6-40c4-987e-e8a138ae8ea0%2Ff4e407ed-ed53-403a-b382-ad554d62e33b%2FUntitled.png%3Fid%3Ddf48ad40-f0c4-4c51-acb2-8133cc744cb7%26table%3Dblock%26spaceId%3Daa032398-6ed6-40c4-987e-e8a138ae8ea0%26expirationTimestamp%3D1719568800000%26signature%3D_rdoXx_uySmUN80peyPBj9SjERv4tRUCuAnN9dLvB38?table=block&id=df48ad40-f0c4-4c51-acb2-8133cc744cb7&cache=v2)
HelloWorldController内容如下:
package com.example.demo.controller; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; /** * hello world * * @author : yi on 2021/6/25 17:36 * @version : 1.0 */ @RestController @RequestMapping("/hello") public class HelloWorldController { @GetMapping public ResponseEntity<String> sayHello() { return ResponseEntity.ok("Hello World!"); } }
7. 启动hello-world项目
右键启动类,启动项目
![notion image](https://www.notion.so/image/https%3A%2F%2Ffile.notion.so%2Ff%2Ff%2Faa032398-6ed6-40c4-987e-e8a138ae8ea0%2F58acb214-488d-40ab-ad37-5ed5ca5a4c57%2FUntitled.png%3Fid%3D5d4497a1-2645-460f-ae7b-44de5d6583b5%26table%3Dblock%26spaceId%3Daa032398-6ed6-40c4-987e-e8a138ae8ea0%26expirationTimestamp%3D1719568800000%26signature%3DpLOM0I9p7A4F_bNfRE-6be6uVMdEvJtx7xJf4tcwc5I?table=block&id=5d4497a1-2645-460f-ae7b-44de5d6583b5&cache=v2)
8. 启动成功
启动成功的标志:Started DemoApplication in 1.057 seconds (JVM running for 1.685) 默认端口:8080
![notion image](https://www.notion.so/image/https%3A%2F%2Ffile.notion.so%2Ff%2Ff%2Faa032398-6ed6-40c4-987e-e8a138ae8ea0%2Feb69d9fe-63d3-4faf-8f1b-3656ef72efa4%2FUntitled.png%3Fid%3Dc1dc4d0a-dada-4ae4-841e-e7f5d02982b8%26table%3Dblock%26spaceId%3Daa032398-6ed6-40c4-987e-e8a138ae8ea0%26expirationTimestamp%3D1719568800000%26signature%3DnPa19Xmht2J694OOdfbyO65HAWZ1mGTp6-eArUh7YhQ?table=block&id=c1dc4d0a-dada-4ae4-841e-e7f5d02982b8&cache=v2)
9. 浏览器验证
打开浏览器输入:localhost:8080/hello 页面上显示 Hello World即成功!至此,一个简答的SpringBoot项目就搭建完成了!
![notion image](https://www.notion.so/image/https%3A%2F%2Ffile.notion.so%2Ff%2Ff%2Faa032398-6ed6-40c4-987e-e8a138ae8ea0%2Fd00f3f93-06db-4056-ab98-82f9706e26c8%2FUntitled.png%3Fid%3Dbc01a2ad-cb91-44e2-9a8b-ab3658c3952b%26table%3Dblock%26spaceId%3Daa032398-6ed6-40c4-987e-e8a138ae8ea0%26expirationTimestamp%3D1719568800000%26signature%3Dd2KKLQW73KaW0fdQG7rLPNfDdTJMYjyPE4bPGyHkBRk?table=block&id=bc01a2ad-cb91-44e2-9a8b-ab3658c3952b&cache=v2)
三、SpringBoot相关网站
感谢阅读,祝您生活愉快!