快速入门
单体 Web 应用
场景1:单体 Web 应用快速开始。
场景1:单体 Web 应用
适合:中小型项目、快速原型、学习使用
步骤1:创建项目
使用 Maven 创建项目:
mvn archetype:generate \
-DgroupId=com.example \
-DartifactId=demo-web \
-DarchetypeArtifactId=maven-archetype-quickstart \
-DinteractiveMode=false
cd demo-web
步骤2:添加依赖
编辑 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
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo-web</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.5.8</version>
</parent>
<properties>
<java.version>21</java.version>
<nebula.version>2.0.1-SNAPSHOT</nebula.version>
</properties>
<dependencies>
<!-- Nebula Web Starter -->
<dependency>
<groupId>io.nebula</groupId>
<artifactId>nebula-starter-web</artifactId>
<version>${nebula.version}</version>
</dependency>
<!-- 测试依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
步骤3:创建配置文件
创建 src/main/resources/application.yml:
spring:
application:
name: demo-web
server:
port: 8080
nebula:
# 启用 Web 支持
web:
enabled: true
# 数据访问配置(如需要)
data:
persistence:
enabled: true
# 缓存配置(如需要)
cache:
enabled: true
type: caffeine
步骤4:创建启动类
创建 src/main/java/com/example/DemoWebApplication.java:
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoWebApplication {
public static void main(String[] args) {
SpringApplication.run(DemoWebApplication.class, args);
}
}
步骤5:创建第一个接口
创建 src/main/java/com/example/controller/HelloController.java:
package com.example.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/hello")
public String hello(@RequestParam(defaultValue = "World") String name) {
return "Hello, " + name + "!";
}
}
步骤6:运行应用
```bash