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. 高级用法

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

相关推荐
左夕19 分钟前
分不清apply,bind,call?看这篇文章就够了
前端·javascript
Zha0Zhun1 小时前
一个使用ViewBinding封装的Dialog
前端
兆子龙1 小时前
从微信小程序 data-id 到 React 列表性能优化:少用闭包,多用 data-*
前端
滕青山1 小时前
文本行过滤/筛选 在线工具核心JS实现
前端·javascript·vue.js
时光不负努力1 小时前
编程常用模式集合
前端·javascript·typescript
恋猫de小郭1 小时前
Apple 的 ANE 被挖掘,AI 硬件公开,宣传的 38 TOPS 居然是"数字游戏"?
前端·人工智能·ios
小岛前端1 小时前
Node.js 宣布重大调整,运行十年的规则要改了!
前端·node.js
OpenTiny社区1 小时前
OpenTiny NEXT-SDK 重磅发布:四步把你的前端应用变成智能应用
前端·javascript·ai编程
梦想CAD控件2 小时前
在线CAD开发包结构与功能说明
前端·javascript·vue.js
张拭心2 小时前
春节后,有些公司明确要求 AI 经验了
android·前端·人工智能