ZetCode

Spring Boot 简介

最后修改于 2023 年 7 月 31 日

本文介绍了 Spring Boot 框架。

Spring

Spring 是一个流行的 Java 应用程序框架,而 Spring Boot 是 Spring 的一个演进,它有助于轻松创建独立的、生产级的基于 Spring 的应用程序。

Spring Boot 是一个用于构建企业级应用程序的完整生态系统。 它包括许多用于构建经典 Web、响应式应用程序和微服务的模块。 Spring Boot 应用程序可以用 Java、Groovy 或 Kotlin 编写。

这些应用程序可以使用 Maven 或 Groovy 构建。

Spring Boot 启动器

Spring Boot 应用程序使用启动器构建。 启动器是一组方便的依赖描述符,我们在应用程序中包含它们。 启动器是所有 Spring 和相关技术的集合,可帮助我们快速设置应用程序。

换句话说,启动器是方便的依赖集合。 例如,spring-boot-starter-data-jdbc 是用于使用 Spring Data JDBC 的启动器。

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
    implementation 'org.springframework.boot:spring-boot-starter-mustache'
    runtimeOnly 'mysql:mysql-connector-java'
}

启动器被添加到 Maven 和 Gradle 构建文件中。

我们可以使用 Spring Initializr 初始化 Spring Boot 应用程序。 它在 https://start.spring.io/ 上作为在线服务提供。 也可以使用 Spring 命令行工具。

Spring Boot 简单示例

我们创建一个简单的 Java Web REST 应用程序。

build.gradle
...
src
├── main
│   ├── java
│   │   └── com
│   │       └── zetcode
│   │           ├── Application.java
│   │           └── controller
│   │               └── MyController.java
│   └── resources
│       ├── application.properties
│       ├── static
│       └── templates
└── test

这是项目结构。

build.gradle
plugins {
    id 'org.springframework.boot' version '3.1.1'
    id 'io.spring.dependency-management' version '1.1.0'
    id 'java'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '17'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
}

这是 Gradle 构建文件。 我们包含 spring-boot-starter-web 启动器,该启动器用于创建经典和 REST Web 应用程序。

resources/application.properties
spring.main.banner-mode=off

application.properties 文件中,我们关闭了横幅。

com/zetcode/controller/MyController.java
package com.zetcode.controller;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MyController {
    
    @GetMapping(value="/status")
    public ResponseEntity<String> status() {

        return ResponseEntity.ok("OK");
    }

    @GetMapping(value="/hello/{name}/")
    public ResponseEntity<String> hello(@PathVariable String name) {

        var msg = String.format("Hello %s!", name);
        return ResponseEntity.ok(msg);
    }
}

我们有两个端点:/status/hello/{{name}}/

@RestController
public class MyController {

为了创建 Restful 应用程序,我们使用 @RestController 注解。

@GetMapping(value="/status")
public ResponseEntity<String> status() {

    return ResponseEntity.ok("OK");
}

我们使用 ResponseEntity 来构建 HTTP 响应。

@GetMapping(value="/hello/{name}/")
public ResponseEntity<String> hello(@PathVariable String name) {

    var msg = String.format("Hello %s!", name);
    return ResponseEntity.ok(msg);
}

我们使用 @PathVariable 注解从路径中获取值。

com/zetcode/Application.java
package com.zetcode;

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

@SpringBootApplication
public class Application {

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

}

Application 中,我们设置应用程序。

@SpringBootApplication
public class Application {

使用 @SpringBootApplication,我们初始化 Spring Boot 应用程序。

$ ./gradlew bootRun

我们启动应用程序。

$ curl localhost:8080/status -i
HTTP/1.1 200
Content-Type: text/plain;charset=UTF-8
Content-Length: 2
Date: Mon, 31 Jul 2023 12:37:25 GMT

OK
$ curl localhost:8080/hello/Peter/
Hello Peter!

我们使用 curl 生成对这两个端点的请求。

在本文中,我们介绍了 Spring Boot 框架。

作者

我叫 Jan Bodnar,我是一位充满激情的程序员,拥有丰富的编程经验。 自 2007 年以来,我一直在撰写编程文章。 迄今为止,我已经撰写了 1,400 多篇文章和 8 本电子书。 我拥有超过十年的编程教学经验。

列出 所有 Spring Boot 教程