运行springboot
目的:
1、了解如何运行springboot;
2、关键运行命令;
3、添加依赖;
实施内容:
下面是一个最简单的 Spring Boot 项目的示例,包括以下内容:
-
使用 Spring Boot 初始化 Web 项目
-
提供一个简单的 RESTful 接口:
/hello
-
使用 Maven 构建
🌱 一、项目结构
含有pom.xml的maven项目。
🧾 二、pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.5.0</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>org.example</groupId><artifactId>demo5</artifactId><version>0.0.1-SNAPSHOT</version><name>demo5</name><description>demo5</description><url/><licenses><license/></licenses><developers><developer/></developers><scm><connection/><developerConnection/><tag/><url/></scm><properties><java.version>17</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
🚀 三、启动类
Demo5Application.java
package com.example.demo;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class Demo5Application {public static void main(String[] args) {SpringApplication.run(Demo5Application.class, args);}}
📡 四、控制器 HelloController.java
package com.example.demo.controller;import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class HelloController {@GetMapping("/hello")public String sayHello() {return "Hello, Spring Boot!";}
}
⚙️ 五、配置文件 application.yml(或使用 application.properties
)
server:port: 8080
含有dependency就是依赖。
✅ 六、运行
-
在项目根目录运行:
mvn spring-boot:run
-
浏览器访问:
http://localhost:8080/hello
应该看到:
Hello, Spring Boot!