IDEA使用Maven方式构建SpringBoot项目
1、环境准备
确保你已经安装了以下工具:
Java JDK(推荐 JDK 8 或更高版本)
IntelliJ IDEA(推荐使用最新版本)
2、创建 Spring Boot 项目
(1) 打开 IntelliJ IDEA。
(2)在欢迎界面,点击 New Project。
(3)在左侧选择 Java。
(4)配置项目信息:如图
(5)点击Create
(6)在生成的 pom.xml 文件中,添加 Spring Boot 依赖:
springboot核心依赖
<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.7.6</version><relativePath/></parent>
其他依赖
<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies>
(7)右键点击项目,选择 Maven -> sync Project,加载依赖。
4、编写代码
在 src/main/java/com/qvtu/web
目录下,编写 Spring Boot 启动入口。
@SpringBootApplication
public class QvtuWeb001Application {public static void main(String[] args) {SpringApplication.run(QvtuWeb001Application.class, args);}}
创建一个简单的 REST 控制器:
在 src/main/java/com/qvtu/web
目录下,创建包名为controller
用来存放controller类
然后创建HelloController
类
package com.qvtu.web.controller;import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class HelloController {@GetMapping("/hello")public String hello(){return "Hello World";}}
5、运行项目
打开 QvtuWeb001Application .java
文件。
点击左侧的绿色三角形按钮,或者右键点击文件,选择 Run 'QvtuWeb001Application '。
项目启动后,控制台会显示 Spring Boot 的启动日志。
6、访问应用程序
如果一切顺利,Spring Boot 应用程序将会启动。你可以通过浏览器访问 http://localhost:8080/hello,看到返回的 Hello World 消息。