SpringBoot 教程 & 笔记 |Demo01- 构建一个简单的 Web 应用程

本贴最后更新于 2026 天前,其中的信息可能已经水流花落

使用 Spring Boot 构建应用程序

本指南提供了 Spring Boot 如何帮助您加速和促进应用程序开发的示例。当您阅读更多 Spring 入门指南时,您将看到更多用于 Spring Boot 的用例。它旨在让您快速了解 Spring Boot。如果您想创建自己的基于 Spring Boot 的项目,请访问 Spring Initializr,填写项目详细信息,选择您的选项,然后您可以下载 Maven 构建文件或捆绑项目作为 zip 文件。

SpringBoot 在建立生产中的独立程序上非常简便、只需要一些简便的配置就能运行起来。
大致有如下特点:

  1. 创建独立的 Spring applications
  2. 能够使用内嵌的 Tomcat, Jetty or Undertow,不需要部署 war
  3. 提供 starter pom 来简化 maven 配置
  4. 自动配置 Spring
  5. 提供一些生产环境的特性,比如 metrics, health checks and externalized configuration
  6. 绝对没有代码生成和 XML 配置要求

你需要什么

*大约 15 分钟
*最喜欢的文本编辑器或 IDE
*JDK 1.8 或更高版本
*Maven 3.2+

IDEA 构建步骤

Open Idea->new Project->Spring Initializr->Group、Artifact、Name->Select web->Finish
填写对应信息就可以了
本示例使用了 SpringBoot 2.1.0 RC1JAVA11 版本
IDEA 构建步骤
IDEA 构建步骤
IDEA 构建步骤
IDEA 构建步骤

目录结构说明

目录结构说明

  • pom 文件为 MAVEN 配置文件
  • resouces 资源文件
    • static 静态资源
    • templates 模板资源
    • application.properties 配置文件
  • Demo01Application 程序的入口

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.heardfate.springboot</groupId>
 <artifactId>demo01</artifactId>
 <version>0.0.1-SNAPSHOT</version>
 <packaging>jar</packaging>
 <name>demo01</name>
 <description>Demo project for Spring Boot</description>
 <parent>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-starter-parent</artifactId>
 <version>2.1.0.RC1</version>
 <relativePath/> <!-- lookup parent from repository -->
 </parent>
 <properties>
 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
 <java.version>11</java.version>
 </properties>
 <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>
 <build>
 <plugins>
 <plugin>
 <groupId>org.springframework.boot</groupId>
 <artifactId>spring-boot-maven-plugin</artifactId>
 </plugin>
 </plugins>
 </build>
 <repositories>
 <repository>
 <id>spring-snapshots</id>
 <name>Spring Snapshots</name>
 <url>https://repo.spring.io/snapshot</url>
 <snapshots>
 <enabled>true</enabled>
 </snapshots>
 </repository>
 <repository>
 <id>spring-milestones</id>
 <name>Spring Milestones</name>
 <url>https://repo.spring.io/milestone</url>
 <snapshots>
 <enabled>false</enabled>
 </snapshots>
 </repository>
 </repositories>
 <pluginRepositories>
 <pluginRepository>
 <id>spring-snapshots</id>
 <name>Spring Snapshots</name>
 <url>https://repo.spring.io/snapshot</url>
 <snapshots>
 <enabled>true</enabled>
 </snapshots>
 </pluginRepository>
 <pluginRepository>
 <id>spring-milestones</id>
 <name>Spring Milestones</name>
 <url>https://repo.spring.io/milestone</url>
 <snapshots>
 <enabled>false</enabled>
 </snapshots>
 </pluginRepository>
 </pluginRepositories>
</project>

创建一个简单的 Web 应用程序

现在,您可以为简单的 Web 应用程序创建 Web 控制器。
src/main/java/com/heardfate/springboot/demo/controller/HelloController.java

package com.heardfate.springboot.demo.controller;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * @since: 2018/10/18
 * @author: Mr.HeardFate
 */
@RestController
public class HelloController {

    @RequestMapping("/")
    public String index() {
        return "Greetings from Spring Boot!";
  }

}

该类被标记为 @RestController,这意味着 Spring MVC 可以使用它来处理 Web 请求。@RequestMapping 映射'/'到 index()方法。从浏览器调用或在命令行上使用 curl 时,该方法返回纯文本。这是因为 @RestController 组合 @Controller 和 @ResponseBody 两个注释会导致 Web 请求返回数据而不是视图。

Demo01Application.java 构建项目的时候系统自动创建的

package com.heardfate.springboot.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Demo01Application {

    public static void main(String[] args) {
        SpringApplication.run(Demo01Application.class, args);
  }
}

@SpringBootApplication 是一个组合注释,添加了以下所有内容:

  • @Configuration 是一个类级注释,指示对象是一个 bean 定义的源。
  • @EnableAutoConfiguration 告诉 Spring Boot 开始根据类路径设置,其他 bean 和各种属性设置添加 bean。
  • 通常你会添加 @EnableWebMvc 一个 Spring MVC 应用程序,但 Spring Boot 会在类路径上看到 spring-webmvc 时自动添加它。这会将应用程序标记为 Web 应用程序并激活关键行为,例如设置 DispatcherServlet。
  • @ComponentScan 告诉 Spring 在包中寻找其他组件,配置和服务 hello,允许它找到控制器。

该 main()方法使用 Spring Boot 的 SpringApplication.run()方法来启动应用程序。您是否注意到没有一行 XML,也没有 web.xml 文件?此 Web 应用程序是 100%纯 Java,您无需处理配置任何管道或基础结构。

创建应用程序
创建应用程序

运行该应用程序

Demo01Application 右击 Run
运行应用程序

查看是否运行成功

命令行下输入:$ curl localhost:8080
可以看到返回了 Greetings from Spring Boot!
查看应用程序

  • Spring

    Spring 是一个开源框架,是于 2003 年兴起的一个轻量级的 Java 开发框架,由 Rod Johnson 在其著作《Expert One-On-One J2EE Development and Design》中阐述的部分理念和原型衍生而来。它是为了解决企业应用开发的复杂性而创建的。框架的主要优势之一就是其分层架构,分层架构允许使用者选择使用哪一个组件,同时为 JavaEE 应用程序开发提供集成的框架。

    941 引用 • 1458 回帖 • 150 关注

相关帖子

欢迎来到这里!

我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。

注册 关于
请输入回帖内容 ...