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. 配置方法


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

复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</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
复制代码
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! 的输出。

4. 高级用法

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

相关推荐
Moshow郑锴9 小时前
实战分享:用 SpringBoot-API-Scheduler 构建 API 监控闭环 —— 从断言验证到智能警报
java·spring boot·后端·任务调度
七淮9 小时前
Next.js SEO 优化完整方案
前端·next.js
e***19359 小时前
爬虫学习 01 Web Scraper的使用
前端·爬虫·学习
aircrushin9 小时前
TRAE SOLO 中国版,正式发布!AI 编程的 "Solo" 时代来了?
前端·人工智能
最初的黄昏9 小时前
flutter 集成flutter_Boost
前端
有意义9 小时前
JavaScript 词法作用域与闭包:从底层原理到实战理解
前端·javascript·面试
GYY_y9 小时前
封装一个支持动态表头与权限控制的通用 VxeTable 组件
前端
某只天落9 小时前
Vue2 通用文件在线预览下载组件:一站式解决多类型文件处理需求(支持视频、文档、图片、Office)
前端
AY呀9 小时前
黑马喽大闹天宫与JavaScript的寻亲记:作用域与作用域链全解析
前端·javascript·面试
金融数据出海9 小时前
日本股票市场渲染 KlineCharts K 线图
前端·后端