使用Spring Boot构建RESTful API:从理论到实践

文章目录

      • 引言
      • 第一章 RESTful API基础知识
        • 1.1 什么是RESTful API
        • 1.2 RESTful API的优势
      • 第二章 Spring Boot基础知识
        • 2.1 什么是Spring Boot
        • 2.2 Spring Boot的主要特性
      • 第三章 使用Spring Boot构建RESTful API
        • 3.1 项目初始化
        • 3.2 构建基础结构
        • 3.3 定义实体类
        • 3.4 创建Repository接口
        • 3.5 实现Service类
        • 3.6 创建Controller类
      • 第四章 Spring Boot高级特性
        • 4.1 Spring Boot Actuator
        • 4.2 Spring Boot与消息队列
      • 第五章 部署与监控
        • 5.1 部署Spring Boot应用
        • 5.2 使用Docker部署Spring Boot应用
        • 5.3 监控Spring Boot应用
      • 第六章 实践案例:构建一个简单的博客平台
        • 6.1 项目结构
        • 6.2 文章管理
      • 结论

. 在这里插入图片描述

引言

RESTful API是Web服务开发中常用的一种架构风格,通过HTTP协议提供与资源交互的方式。Spring Boot作为一个流行的Java框架,通过简化配置和快速开发,成为构建RESTful API的理想选择。本文将深入探讨如何使用Spring Boot构建RESTful API,包括基础知识、核心功能、最佳实践和实际应用,并提供具体的代码示例和应用案例。

第一章 RESTful API基础知识

1.1 什么是RESTful API

RESTful API是一种基于REST(Representational State Transfer)架构风格的Web服务接口,通过使用HTTP协议进行资源的创建、读取、更新和删除(CRUD)操作。RESTful API具有以下特点:

  • 资源(Resource):API中的每个实体都是一个资源,通过URI(统一资源标识符)来标识。
  • HTTP动词:使用HTTP动词(GET、POST、PUT、DELETE)进行操作,表示对资源的不同操作。
  • 状态表示:资源的状态通过表述(Representation)来传递,可以是JSON、XML等格式。
  • 无状态:每个请求都是独立的,不依赖于之前的请求状态。
1.2 RESTful API的优势
  • 简单易用:通过HTTP协议和标准化的动词操作,简单易用。
  • 灵活性:支持多种数据格式和通信方式,灵活性高。
  • 可扩展性:支持分布式系统的扩展和集成,适合大规模应用。

第二章 Spring Boot基础知识

2.1 什么是Spring Boot

Spring Boot是一个基于Spring框架的开源项目,提供了一种快速构建生产级Spring应用的方法。通过简化配置、提供自动化配置(Auto Configuration)和内嵌服务器等特性,Spring Boot使得开发者能够更加专注于业务逻辑,而不需要花费大量时间在配置和部署上。

2.2 Spring Boot的主要特性
  • 自动化配置:通过自动化配置减少了大量的手动配置工作。
  • 内嵌服务器:提供内嵌的Tomcat、Jetty和Undertow服务器,方便开发者在开发和测试阶段快速启动和运行应用。
  • 独立运行:应用可以打包成一个可执行的JAR文件,包含所有依赖项,可以独立运行,不需要外部的应用服务器。
  • 生产级功能:提供了监控、度量、健康检查等生产级功能,方便开发者管理和监控应用的运行状态。
  • 多样化的配置:支持多种配置方式,包括YAML、Properties文件和环境变量,满足不同开发和部署环境的需求。

第三章 使用Spring Boot构建RESTful API

3.1 项目初始化

使用Spring Initializr生成一个Spring Boot项目,并添加所需依赖。

<!-- 示例:通过Spring Initializr生成的pom.xml配置文件 -->
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>rest-api</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>rest-api</name>
    <description>Demo project for Spring Boot RESTful API</description>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>
3.2 构建基础结构

在项目中创建必要的包结构,包括controller、service、repository和model。

rest-api
├── src
│   ├── main
│   │   ├── java
│   │   │   └── com
│   │   │       └── example
│   │   │           └── restapi
│   │   │               ├── RestApiApplication.java
│   │   │               ├── controller
│   │   │               │   └── UserController.java
│   │   │               ├── service
│   │   │               │   └── UserService.java
│   │   │               ├── repository
│   │   │               │   └── UserRepository.java
│   │   │               └── model
│   │   │                   └── User.java
│   │   └── resources
│   │       ├── application.yml
│   │       └── data.sql
└── pom.xml
3.3 定义实体类

定义一个简单的用户实体类,并配置JPA注解。

// 示例:用户实体类
package com.example.restapi.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String name;
    private String email;

    // Getters and Setters
}
3.4 创建Repository接口

创建一个JPA Repository接口,用于数据访问操作。

// 示例:用户Repository接口
package com.example.restapi.repository;

import com.example.restapi.model.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface UserRepository extends JpaRepository<User, Long> {
}
3.5 实现Service类

创建一个Service类,封装业务逻辑和数据访问操作。

// 示例:用户服务类
package com.example.restapi.service;

import com.example.restapi.model.User;
import com.example.restapi.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class UserService {
    @Autowired
    private UserRepository userRepository;

    public List<User> findAll() {
        return userRepository.findAll();
    }

    public Optional<User> findById(Long id) {
        return userRepository.findById(id);
    }

    public User save(User user) {
        return userRepository.save(user);
    }

    public void deleteById(Long id) {
        userRepository.deleteById(id);
    }
}
3.6 创建Controller类

创建一个Controller类,定义RESTful API的端点,并通过Service类处理请求。

// 示例:用户控制器
package com.example.restapi.controller;

import com.example.restapi.model.User;
import com.example.restapi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/api/users")
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping
    public List<User> getAllUsers() {
        return userService.findAll();
    }

    @GetMapping("/{id}")
    public Optional<User> getUserById(@PathVariable Long id) {
        return userService.findById(id);
    }

    @PostMapping
    public User createUser(@RequestBody User user) {
        return userService.save(user);
    }

    @PutMapping("/{id}")
    public User updateUser(@PathVariable Long id, @RequestBody User userDetails) {
        User user = userService.findById(id).orElseThrow(() -> new ResourceNotFoundException("User not found with id " + id));
        user.setName(userDetails.getName());
        user.setEmail(userDetails.getEmail());
       

 return userService.save(user);
    }

    @DeleteMapping("/{id}")
    public void deleteUser(@PathVariable Long id) {
        userService.deleteById(id);
    }
}

第四章 Spring Boot高级特性

4.1 Spring Boot Actuator

Spring Boot Actuator提供了一组用于监控和管理Spring Boot应用的功能,通过一组内置的端点,开发者可以方便地获取应用的运行状态、健康检查、度量指标等信息。

# 示例:启用Spring Boot Actuator
management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: always
// 示例:自定义健康检查
package com.example.restapi.actuator;

import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;

@Component
public class CustomHealthIndicator implements HealthIndicator {
    @Override
    public Health health() {
        // 自定义健康检查逻辑
        return Health.up().withDetail("status", "All systems operational").build();
    }
}
4.2 Spring Boot与消息队列

Spring Boot与消息队列(如RabbitMQ、Kafka)结合,可以实现异步处理和事件驱动的架构。

<!-- 示例:集成RabbitMQ的pom.xml配置文件 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
// 示例:RabbitMQ配置类
package com.example.restapi.config;

import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {
    @Bean
    public Queue myQueue() {
        return new Queue("myQueue", false);
    }
}

// 示例:消息发送服务
package com.example.restapi.service;

import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class MessageSender {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void sendMessage(String message) {
        rabbitTemplate.convertAndSend("myQueue", message);
    }
}

// 示例:消息接收服务
package com.example.restapi.service;

import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

@Service
public class MessageReceiver {
    @RabbitListener(queues = "myQueue")
    public void receiveMessage(String message) {
        System.out.println("Received message: " + message);
    }
}

第五章 部署与监控

5.1 部署Spring Boot应用

Spring Boot应用可以通过多种方式进行部署,包括打包成JAR文件、Docker容器、Kubernetes集群等。

# 打包Spring Boot应用
mvn clean package

# 运行Spring Boot应用
java -jar target/rest-api-0.0.1-SNAPSHOT.jar
5.2 使用Docker部署Spring Boot应用

Docker是一个开源的容器化平台,可以帮助开发者将Spring Boot应用打包成容器镜像,并在任何环境中运行。

# 示例:Dockerfile文件
FROM openjdk:11-jre-slim
VOLUME /tmp
COPY target/rest-api-0.0.1-SNAPSHOT.jar app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
# 构建Docker镜像
docker build -t spring-boot-rest-api .

# 运行Docker容器
docker run -p 8080:8080 spring-boot-rest-api
5.3 监控Spring Boot应用

Spring Boot Actuator提供了丰富的监控功能,通过Prometheus和Grafana,可以实现对Spring Boot应用的监控和可视化。

<!-- 示例:集成Prometheus的pom.xml配置文件 -->
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
# 示例:Prometheus配置文件
management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    prometheus:
      enabled: true

第六章 实践案例:构建一个简单的博客平台

6.1 项目结构

本节将通过一个简单的博客平台案例,展示Spring Boot在实际应用中的使用,包括文章管理、用户管理和评论管理等功能。

blog-platform
├── src
│   ├── main
│   │   ├── java
│   │   │   └── com
│   │   │       └── example
│   │   │           └── blog
│   │   │               ├── BlogApplication.java
│   │   │               ├── controller
│   │   │               │   ├── ArticleController.java
│   │   │               │   ├── UserController.java
│   │   │               │   └── CommentController.java
│   │   │               ├── service
│   │   │               │   ├── ArticleService.java
│   │   │               │   ├── UserService.java
│   │   │               │   └── CommentService.java
│   │   │               ├── repository
│   │   │               │   ├── ArticleRepository.java
│   │   │               │   ├── UserRepository.java
│   │   │               │   └── CommentRepository.java
│   │   │               └── model
│   │   │                   ├── Article.java
│   │   │                   ├── User.java
│   │   │                   └── Comment.java
│   │   └── resources
│   │       ├── application.yml
│   │       └── data.sql
└── pom.xml
6.2 文章管理

文章管理包括文章的创建、更新、删除和查询。

// 示例:文章实体类
package com.example.blog.model;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Article {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private String title;
    private String content;

    // Getters and Setters
}

// 示例:文章Repository接口
package com.example.blog.repository;

import com.example.blog.model.Article;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface ArticleRepository extends JpaRepository<Article, Long> {
}

// 示例:文章服务类
package com.example.blog.service;

import com.example.blog.model.Article;
import com.example.blog.repository.ArticleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class ArticleService {
    @Autowired
    private ArticleRepository articleRepository;

    public List<Article> findAll() {
        return articleRepository.findAll();
    }

    public Optional<Article> findById(Long id) {
        return articleRepository.findById(id);
    }

    public Article save(Article article) {
        return articleRepository.save(article);
    }

    public void deleteById(Long id) {
        articleRepository.deleteById(id);
    }
}

// 示例:文章控制器
package com.example.blog.controller;

import com.example.blog.model.Article;
import com.example.blog.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/api/articles")
public class ArticleController {
    @Autowired
    private ArticleService articleService;

    @GetMapping
    public List<Article> getAllArticles() {
        return articleService.findAll();
    }

    @GetMapping("/{id}")
    public Optional<Article> getArticleById(@PathVariable Long id) {
        return articleService.findById(id);
    }

    @PostMapping
    public Article createArticle(@RequestBody Article article) {
        return articleService.save(article);
    }

    @PutMapping("/{id}")
    public Article updateArticle(@PathVariable Long id, @RequestBody Article articleDetails) {
        Article article = articleService.findById(id).orElseThrow(() -> new ResourceNotFoundException("Article not found with id " + id));
        article.setTitle(articleDetails.getTitle());
        article.setContent(articleDetails.getContent());
        return articleService.save(article);
    }

    @DeleteMapping("/{id}")
    public void deleteArticle(@PathVariable Long id) {
        articleService.deleteById(id);
    }
}

结论

通过Spring Boot,开发者可以高效地构建一个RESTful API,涵盖用户管理、文章管理等关键功能。本文详细介绍了RESTful API的基础知识、Spring Boot的核心功能、高级特性以及实践案例,帮助读者深入理解和掌握Spring Boot在RESTful API开发中的应用。希望本文能够为您进一步探索和应用Spring Boot提供有价值的参考。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mfbz.cn/a/746170.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

虚拟化技术(一)

目录 一、虚拟化技术简介二、服务器虚拟化&#xff08;一&#xff09;服务器虚拟化的层次&#xff08;二&#xff09;服务器虚拟化的底层实现&#xff08;三&#xff09;虚拟机迁移&#xff08;四&#xff09;隔离技术&#xff08;五&#xff09;案例分析 一、虚拟化技术简介 虚…

【十六】【QT开发应用】Menu菜单,contextMenuEvent,setContextMenuPolicy,addAction

在 Qt 框架中&#xff0c;QMenu 类用于创建和管理菜单。菜单是用户界面的一部分&#xff0c;可以包含多个选项或动作&#xff0c;用户可以选择这些选项来执行特定的功能。菜单通常显示在菜单栏、上下文菜单&#xff08;右键菜单&#xff09;或工具栏中。 基本用法 创建菜单对象…

# Kafka_深入探秘者(5):kafka 分区

Kafka_深入探秘者&#xff08;5&#xff09;&#xff1a;kafka 分区 一、kafka 副本机制 1、Kafka 可以将主题划分为多个分区(Partition)&#xff0c;会根据分区规则选择把消息存储到哪个分区中&#xff0c;只要如果分区规则设置的合理&#xff0c;那么所有的消息将会被均匀的…

边缘混合计算智慧矿山视频智能综合管理方案:矿山安全生产智能转型升级之路

一、智慧矿山方案介绍 智慧矿山是以矿山数字化、信息化为前提和基础&#xff0c;通过物联网、人工智能等技术进行主动感知、自动分析、快速处理&#xff0c;实现安全矿山、高效矿山的矿山智能化建设。旭帆科技TSINGSEE青犀基于图像的前端计算、边缘计算技术&#xff0c;结合煤…

u盘插到另一台电脑上数据丢失怎么办?提供实用的解决方案

在现代数字化生活中&#xff0c;U盘作为一种便携式存储设备&#xff0c;承载着我们重要的数据和信息。然而&#xff0c;有时当我们将U盘插入另一台电脑时&#xff0c;可能会遇到数据丢失的棘手问题。这可能是由于多种原因造成的&#xff0c;那么&#xff0c;U盘插到另一台电脑上…

使用隐式事件执行控制图

什么是隐式事件&#xff1f; 隐式事件是图表执行时发生的内置事件&#xff1a; 图表唤醒 进入一个状态 退出状态 分配给内部数据对象的值 这些事件是隐式的&#xff0c;因为您没有显式地定义或触发它们。隐式事件是它们发生的图表的子级&#xff0c;仅在父图表中可见。 隐式事…

png格式快速压缩该怎么做?在电脑压缩png图片的方法

png格式的图片如何快速压缩变小呢&#xff1f;现在网络的不断发展&#xff0c;图片是日常用来分享展示内容的一种常用手段&#xff0c;其中使用最多的一种图片格式就是png&#xff0c;png格式具有无损压缩支持透明底的特性&#xff0c;在很多的场景下都会使用。 现在图片的清晰…

笔记-python reduce 函数

reduce() 函数在 python 2 是内置函数&#xff0c; 从python 3 开始移到了 functools 模块。 官方文档是这样介绍的 reduce(...) reduce(function, sequence[, initial]) -> valueApply a function of two arguments cumulatively to the items of a sequence, from left …

健身房管理系统

摘 要 随着人们健康意识的增强&#xff0c;健身房作为一种提供健身服务的场所&#xff0c;受到越来越多人的关注和喜爱。然而&#xff0c;传统的健身房管理方式存在诸多问题&#xff0c;如信息管理不便捷、会员管理不规范等。为了解决这些问题&#xff0c;本文设计并实现了一款…

分享一套基于SSM的九宫格日志网站(源码+文档+部署)

大家好&#xff0c;今天给大家分享一套基于SSM的九宫格日志网站 开发语言&#xff1a;Java 数据库&#xff1a;MySQL 技术&#xff1a;SpringSpringMvcMyBatis 工具&#xff1a;IDEA/Ecilpse、Navicat、Maven 博主介绍&#xff1a; 一名Java全栈工程师&#xff0c;专注于Java全…

AI大模型日报#0626:首款大模型芯片挑战英伟达、面壁智能李大海专访、大模型测试题爆火LeCun点赞

导读&#xff1a;AI大模型日报&#xff0c;爬虫LLM自动生成&#xff0c;一文览尽每日AI大模型要点资讯&#xff01;目前采用“文心一言”&#xff08;ERNIE-4.0-8K-latest&#xff09;生成了今日要点以及每条资讯的摘要。欢迎阅读&#xff01;《AI大模型日报》今日要点&#xf…

vue elementui简易侧拉栏的使用

如图所示&#xff0c;增加了侧拉栏&#xff0c;目的是可以选择多条数据展示数据 组件&#xff1a; celadon.vue <template><div class"LayoutMain"><el-aside :width"sidebarIsCollapse ? 180px : 0px" class"aside-wrap">…

MD5加密接口

签名算法 app_key和app_secret由对方系统提供 MD5_CALCULATE_HASH_FOR_CHAR&#xff08;中文加密与JAVA不一致&#xff09; 代码&#xff1a; *获取传输字段名的ASCII码&#xff0c;根据ASCII码对字段名进行排序SELECT * FROM zthr0051WHERE functionid iv_functionidINTO …

AI音乐大模型:深度剖析创意与产业的双重变革

随着AI技术的飞速发展&#xff0c;音乐大模型在最近一个月内纷纷上线&#xff0c;这一变革性技术不仅颠覆了传统的音乐创作方式&#xff0c;更是对整个音乐产业及创意产业带来了深远的影响。本文将从多个维度出发&#xff0c;深度剖析AI音乐大模型对创意与产业的双重变革。 一、…

多模态能力评估新篇章:MMStar引领大型视觉语言模型评估新标准

随着大模型&#xff08;LLMs&#xff09;的快速发展&#xff0c;将视觉模态整合进LLMs以提升模型的交互能力已成为研究的热点。这些大型视觉语言模型&#xff08;LVLMs&#xff09;不仅展现出强大的视觉感知和理解能力&#xff0c;还能够通过对话与用户互动&#xff0c;提供更丰…

Matlab|【免费】含氢气氨气综合能源系统优化调度

目录 主要内容 部分代码 结果一览 下载链接 主要内容 该程序参考《_基于氨储能技术的电转氨耦合风–光–火综合能源系统双层优化调度》模型&#xff0c;对制氨工厂、风力发电、电制氢、燃气轮机、火电机组等主体进行建模分析&#xff0c;以火电机组启停成本、煤耗…

尚硅谷vue2的todolist案例解析,基本概括了vue2所有知识点,结尾有具体代码,复制粘贴学习即可

脚手架搭建 1-初始化脚手架&#xff08;全局安装&#xff09; npm install -g vue/cli2-切换到创建项目的空目录下 vue create xxxx整体结构 整体思路 App定义所有回调方法 增删改查 还有统一存放最终数据&#xff0c;所有子组件不拿数据&#xff0c;由App下发数据&#xf…

Spring Boot 集成 H2 数据库

1. 引言 Spring Boot 以其简洁的配置和快速开发能力&#xff0c;成为现代微服务架构的首选框架之一。而H2数据库作为一个轻量级的内存数据库&#xff0c;非常适合开发阶段作为嵌入式数据库进行单元测试和功能验证。本文将手把手教你如何在Spring Boot项目中集成H2数据库&#…

Mybatis 到 MyBatisPlus

Mybatis 到 MyBatisPlus Mybatis MyBatis&#xff08;官网&#xff1a;https://mybatis.org/mybatis-3/zh/index.html &#xff09;是一款优秀的 持久层 &#xff08;ORM&#xff09;框架&#xff0c;用于简化JDBC的开发。是 Apache的一个开源项目iBatis&#xff0c;2010年这…

DC/AC电源模块一种效率与可靠性兼备的能源转换解决方案

DC/AC电源模块都是一种效率与可靠性兼备的能源转换解决方案 DC/AC电源模块是一种能够将直流电源&#xff08;DC&#xff09;转换为交流电源&#xff08;AC&#xff09;的设备。它在现代电子设备中扮演着非常重要的角色&#xff0c;因为许多设备需要交流电源才能正常运行。无论…