SpringBoot 依赖之Spring Web

SpringBoot 依赖之 Spring Web

详细介绍 Spring Web 依赖的内容:

第 1 章:Spring Web

1. 简介

功能描述

  • 英文: Build web, including RESTful, applications using Spring MVC. Uses Apache Tomcat as the default embedded container.
  • 中文译文:使用 Spring MVC 构建 Web 应用程序,包括 RESTful 应用程序。使用 Apache Tomcat 作为默认嵌入式容器。

Spring Web 提供了构建 Web 应用的基础框架。它支持创建基于 Servlet 的 Web 应用,以及基于 Reactive Streams 的 WebFlux 应用。通过使用 Spring Web,开发者可以轻松构建 RESTful API 和 Web 应用。

2. 配置方法

SpringBoot 依赖之Spring Web_第1张图片
SpringBoot 依赖之Spring Web_第2张图片

pom.xml 文件中添加 Spring Web 依赖:

<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-webartifactId>
dependency>
3. 基本用法

下面我们以一个简单的 Spring Boot Web 应用示例,来展示如何创建一个基本的 RESTful API。可以按照代码操作,倾囊输出,源码后期会推送到github或gitee分享。

3.1. 项目结构
springboot-web
│   ├── src
│   │   ├── main
│   │   │   ├── java
│   │   │   │   └── com
│   │   │   │       └── dependencies
│   │   │   │           └── springweb
│   │   │   │               │── SpringWebApplication.java
│   │   │   │               └── controller
│   │   │   │               	└── HelloController.java
│   │   │   └── resources
│   │   │       └── application.properties
│   └── pom.xml
3.2. SpringWebApplication.java

SpringBoot 依赖之Spring Web_第3张图片

package com.dependencies.springweb;

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

@SpringBootApplication
public class SpringWebApplication {
    
    public static void main(String[] args) {
        SpringApplication.run(SpringWebApplication.class, args);
    }
    
}
3.3. HelloController.java
package com.dependencies.springweb.controller;

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

/**
 * @author zhizhou   2024/7/23 22:52
 */
@RestController
public class HelloController {
    
    @GetMapping("/hello")
    public String sayHello() {
        return "Hello, World!";
    }
}
3.4. 启动应用

运行 SpringWebApplication.java,然后在浏览器中访问 http://localhost:8080/hello,你会看到 Hello, World! 的输出。

SpringBoot 依赖之Spring Web_第4张图片

4. 高级用法

…有点困,先到这,明天补齐

你可能感兴趣的:(微服务架构设计,spring,spring,boot,maven,intellij-idea)