Spring Boot RESTful API开发教程

embedded/2024/10/5 20:14:22/
一、RESTful API简介

RESTful API是一种基于HTTP协议的Web API,其设计原则是简单、可扩展、轻量级、可缓存、可靠、可读性强。RESTful API通常使用HTTP请求方法(GET、POST、PUT、DELETE等)来操作资源,使用HTTP状态码来表示操作结果,使用JSON或XML等格式来传输数据。
在这里插入图片描述

二、Spring Boot简介

Spring Boot是一个基于Spring框架的快速开发Web应用程序的工具。它提供了一种快速、简单、灵活的方式来构建Web应用程序,可以帮助开发人员快速搭建一个基于Spring的Web应用程序,而不需要进行大量的配置和代码编写。

三、使用Spring Boot构建RESTful API
  1. 创建Spring Boot项目

    可以使用Spring Initializr来创建一个基本的Spring Boot项目,也可以使用Eclipse或IntelliJ IDEA等集成开发环境来创建项目。在创建项目后,需要添加一些依赖来支持RESTful API的开发。

    pom.xml文件中添加以下依赖:

    <dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId></dependency>
    </dependencies>
    

    其中,spring-boot-starter-web依赖提供了Spring MVC和Tomcat等Web开发所需的依赖,jackson-databind依赖提供了JSON序列化和反序列化的支持。

  2. 编写后端业务逻辑

    以创建一个管理用户信息的简单API为例。

    • 创建一个实体类User

      package com.example.demo;public class User {private Long id;private String name;private String email;// Getters and setters
      }
      
    • 创建一个仓库接口UserRepository,用于数据的CRUD操作:

      package com.example.demo;import org.springframework.data.jpa.repository.JpaRepository;public interface UserRepository extends JpaRepository<User, Long> {
      }
      
    • 创建一个控制器类UserController,使用@RestController注解:

      package com.example.demo;import java.util.List;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.web.bind.annotation.*;@RestController
      @RequestMapping("/users")
      public class UserController {private final UserRepository repository;@Autowiredpublic UserController(UserRepository repository) {this.repository = repository;}@GetMapping("/")public List<User> getUsers() {return repository.findAll();}@PostMapping("/")public User createUser(@RequestBody User user) {return repository.save(user);}@GetMapping("/{id}")public User getUser(@PathVariable Long id) {return repository.findById(id).orElseThrow(() -> new RuntimeException("User not found"));}@PutMapping("/{id}")public User updateUser(@PathVariable Long id, @RequestBody User userDetails) {User user = repository.findById(id).orElseThrow(() -> new RuntimeException("User not found"));user.setName(userDetails.getName());user.setEmail(userDetails.getEmail());return repository.save(user);}@DeleteMapping("/{id}")public void deleteUser(@PathVariable Long id) {repository.deleteById(id);}
      }
      
  3. 运行应用程序并测试RESTful API

    可以使用Postman等工具来测试API的各种请求方法和参数。

    • GET /users:返回所有用户的列表。
    • POST /users:创建一个新用户。
    • GET /users/{id}:返回指定id的用户。
    • PUT /users/{id}:更新指定id的用户。
    • DELETE /users/{id}:删除指定id的用户。

课程推荐

诚邀你关注我的精品视频课程《ChatGPT+AI项目实战,打造多端智能虚拟数字人》。
课程以项目实战的方式,基于ChatGPT完成多端全栈式开发,实现AI绘画、智能语音、数字虚拟人等,从0到1手把手带你打造一个专属对话虚拟人。通过语音唤醒、识别及合成、安卓开发、前后端快速搭建等技术,使你具备将AI技术真实落地工作中,高效快速提高自身核心竞争力。使用Spring Boot构建了一个简单的RESTful API。这个API提供了对用户信息的CRUD操作,并且可以通过HTTP请求方法来访问。
在这里插入图片描述


http://www.ppmy.cn/embedded/123528.html

相关文章

第九章---for循环及在STL的应用(vector\map\set\list\for_each)、嵌套while、while 统一输出、do-while

在C中&#xff0c;循环语句用于重复执行一段代码&#xff0c;直到指定的条件不再满足。C 提供了几种循环机制&#xff0c;下面将详细讲解每种循环语句的用法和特点。 1. for 循环 for 循环是最常用的循环结构之一&#xff0c;它有三种基本形式&#xff1a; 基本形式&#xf…

Qt_QSS介绍与使用

目录 1、QSS的语法介绍 2、QSS的基本使用 3、QSS的全局设置 4、样式的叠加特性 5、样式的优先级 6、使用Qt Designer设置样式 7、选择器种类介绍 7.1 类选择器 7.2 ID选择器 7.3 并集选择器 8、子控件选择器 9、伪类选择器 10、盒子模型 10.1 设置边框和内…

Python 封装 socket 为 [TCP/UDP/MULTICAST] 服务端

在新线程中创建 TCP/UDP/MULTICAST 协议的服务端套接字&#xff0c;接收客户端的连接请求或数据&#xff0c;并调用 on_recv 回调函数处理数据。 #!/usr/bin/env python # -*- coding: utf-8 -*- import socket import threading import multiprocessingclass ServerSocket:de…

01_OpenCV图片读取与展示

import cv2 img cv2.imread(夕阳.jpg, 1) #cv2.imshow(image, img) #此行只能命令行处py文件执行&#xff0c;会弹出一个视频窗口 #cv2.waitKey (0)以下会在jupyter Lab控件中显示读取的图像 #bgr8转jpeg格式 import enum import cv2def bgr8_to_jpeg(value, quality75):ret…

hystrix微服务部署

目录 一.启动nacos和redis 1.查看是否有nacos和redis 二.开始项目 1.hystrix1工程&#xff08;修改一下工程的注册名字&#xff09; 2.运行登录nacos网站查看运行效果&#xff08;默认密码nacos,nacos&#xff09; 3.开启第二个项目 hystrix2工程 4.关闭第二个项目 hyst…

论文 | Model-tuning Via Prompts Makes NLP Models Adversarially Robust

这篇论文研究了使用提示 (Prompting) 方法微调预训练语言模型&#xff0c;以提高其在对抗样本攻击下的鲁棒性。论文的主要贡献如下&#xff1a; 1.MVP 比 MLP-FT 更鲁棒&#xff1a; 论文比较了 MVP (Model-tuning Via Prompts) 和传统的 MLP-FT (Fine-tuning with an MLP head…

鸿蒙HarmonyOS NEXT 电商APP开发,打造你的专属购物商城

2024年年初&#xff0c;鸿蒙HarmonyOS Next星河版强势发布&#xff0c;随着鸿蒙系统的普及和应用场景的拓展&#xff0c;市场需求将持续增加。鸿蒙系统已经应用于华为的智能手机、平板电脑、智能家居等多个领域&#xff0c;并有望在未来拓展到智能汽车、物联网等更多领域。这为…

记一次vue路由跳转登陆之前的页面,参数丢失问题

一、背景 vue3.0&#xff0c;项目登陆之前访问某个可访问的页面&#xff0c;当跳转到需要登陆才能访问的页面时&#xff0c;跳转到登陆页面&#xff0c;登陆后再跳转到登陆之前需要登陆才能访问的页面&#xff0c;跳转时发现参数丢失了。 A页面&#xff08;无需登陆&#xff…