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

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

相关推荐
Csvn8 小时前
WASM
前端
修己xj9 小时前
我用 AI 给自己写了一个 Markdown 编辑器,然后把它开源了
前端
FRing039 小时前
vue2干货总结2(v-bind、v-for、v-model、指令修饰符、v-bind操作class和style)
开发语言·前端·vue.js·vue2
幸运小猿10 小时前
模拟慢上传-处理超时重试image 丢失线上问题
前端·javascript·html
moshuying11 小时前
网购能买Token了!
前端
h_654321011 小时前
echarts实现柱状图加一条横线
前端·echarts
霸道流氓气质11 小时前
AI 流式对话与实时聊天平台:SSE 解码竞态、有状态会话隔离与背压取消的 Vue3 + Spring AI 生产级实战
java·人工智能·spring
云创智城-yuncitys11 小时前
YunParking 4.0:JDK 17 与 Spring Boot 3.2 的停车云升级
java·开发语言·spring boot
风骏时光牛马11 小时前
生成一个后端标题,只需要一个标题,不需要其他的任何文本。
前端
子非鱼@Itfuture11 小时前
关于前后端联调中登录状态/登录信息失效?
java·前端