1. 通过maven创建springboot程序
输入 mvn archetype:generate
选择模板,一般默认选择为第 7 种方式;
选择之后,一般要你填写如下内容:
- groupId: 组织名称;
- artifactId: 项目名称;
- version: 版本(或快照);
- package: 与组织名称一致即可(打包后的名称);
填写完之后在键盘上按y再回车即可;
创建成功之后,输入cd 项目名称 进入项目目录下,然后输入 code . 即可之间在VScode集成环境下,打开项目;
2. pom.xml 配置
<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</artifactId>
<version>0.0.1</version>
<packaging>jar</packaging>
<name>demo</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<!-- 新增 parent 节点 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.4</version>
</parent>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- 新增 spring-boot-starter-web 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>
3. 编写启动类
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Hello world!
*
*/
@SpringBootApplication
@RestController
public class App
{
@GetMapping("/")
public String hello() {
return "Hello World!";
}
public static void main( String[] args )
{
SpringApplication.run(App.class, args);
}
}
4. 测试
在终端中输入 mvn spring-boot:run 启动 springboot 程序;
在浏览器中输入 localhost:8080