创建项目
创建一个普通 Maven 项目。
添加依赖和插件
Spring Boot 项目必须要将 parent 设置为 spring-boot-starter-parent
,该 Parent 包含了大量默认配置,简化程序开发。
pom.xml1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| <?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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.6.6</version> </parent>
<groupId>org.example</groupId> <artifactId>shsxt-springboot-quickstart</artifactId> <version>1.0-SNAPSHOT</version>
<name>shsxt-springboot-quickstart</name> <url>http://www.example.com</url>
<properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <maven.compiler.source>1.8</maven.compiler.source> <maven.compiler.target>1.8</maven.compiler.target> </properties>
<dependencies> <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>
|
代码实现
创建启动程序
1 2 3 4 5 6 7 8 9 10 11
| package org.example;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication public class Starter { public static void main(String[] args) { SpringApplication.run(Starter.class); } }
|
创建控制器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| package org.example.controller;
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.ResponseBody;
@Controller @ResponseBody public class HelloController {
@GetMapping("hello") public String hello(){ return "hello world!!!"; } }
|
项目结构