【实战指南】RESTful 从入门到精通(Spring Boot)

2024-08-30 01:12

本文主要是介绍【实战指南】RESTful 从入门到精通(Spring Boot),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

文章目录

  • RESTful 从入门到进阶
    • 引言
    • 第一部分:基础知识
      • 1. REST 概念介绍
      • 2. HTTP 方法和状态码
      • 3. RESTful 资源与URI
    • 第二部分:Java 实现 RESTful API
      • 4. Spring Boot 快速搭建 RESTful 服务
      • 5. 创建 RESTful 控制器
      • 6. 使用 JPA 进行数据持久化
    • 第三部分:高级主题
      • 7. 认证与授权
      • 8. 异常处理
      • 9. 测试 RESTful API

RESTful 从入门到进阶

引言

RESTful API 已经成为现代 Web 服务的标准之一,无论是在移动应用、Web 应用还是物联网领域都有着广泛的应用。掌握 RESTful API 的设计和实现对于任何前端或后端开发者来说都是必不可少的技能之一。

  • RESTful API 的重要性与应用场景
    • RESTful API 作为现代 Web 服务的核心,为客户端与服务器之间的交互提供了一种简洁、灵活的方式。
    • RESTful API 广泛应用于各种场景,包括移动应用、Web 应用、物联网设备等,它允许不同系统之间进行无缝的数据交换。
  • RESTful 设计原则简介
    • RESTful API 遵循 REST (Representational State Transfer) 架构风格,这是一种轻量级、无状态的服务交互方式。
    • RESTful 设计强调使用标准的 HTTP 方法(如 GET, POST, PUT, DELETE)来表示对资源的操作,并利用 URL 来唯一标识资源。
  • 为什么选择 Java 和 Spring Boot?
    • Java 是一种广泛使用的编程语言,特别是在企业级应用中。
    • Spring Boot 是基于 Spring 框架的一个简化版本,它提供了快速开发 RESTful 服务的能力,同时也简化了配置和依赖管理。
    • Spring Boot 内置了对 RESTful API 的支持,使得开发者能够快速构建可维护的 Web 服务。

第一部分:基础知识

1. REST 概念介绍

1. REST 定义

  • REST 是一种用于构建 Web 服务的设计风格,它的核心理念是将网络应用视为一组资源,这些资源通过统一的接口被访问和操作。
  • RESTful API 遵循 REST 架构风格,它是一种无状态的、客户端-服务器的、基于超文本传输协议 (HTTP) 的架构风格。
    2. RESTful 架构的关键特点
  • 无状态:每次请求都包含所有必要的信息,服务器不会存储客户端的状态。
  • 客户端-服务器模型:客户端和服务端分离,客户端负责显示和用户交互,服务端负责数据管理和业务逻辑。
  • 缓存:响应可以被缓存以提高性能。
  • 分层系统:中间层可以缓存数据,提高安全性。
  • 统一接口:通过标准的 HTTP 方法和状态码进行交互。
    3. RESTful 与 SOAP 的对比
  • RESTful API 通常使用简单的 HTTP 方法进行操作,而 SOAP 则是一种更为复杂的协议,使用 XML 格式进行消息传输。
  • RESTful API 更易于理解,更轻量级,更适合互联网应用;SOAP 更适合企业级应用,特别是那些需要事务保证的应用场景。

2. HTTP 方法和状态码

1. GET, POST, PUT, DELETE 等方法的用途

  • GET: 用于检索资源的信息。
  • POST: 用于创建新的资源。
  • PUT: 用于更新现有资源。
  • DELETE: 用于删除资源。
    2. 常见 HTTP 状态码解释
  • 200 OK: 请求成功。
  • 201 Created: 资源创建成功。
  • 204 No Content: 成功但没有返回数据。
  • 400 Bad Request: 客户端发送的请求有误。
  • 401 Unauthorized: 请求未经授权。
  • 403 Forbidden: 请求被拒绝。
  • 404 Not Found: 请求的资源不存在。
  • 500 Internal Server Error: 服务器内部错误。

3. RESTful 资源与URI

1. 资源定义

  • 在 RESTful API 中,资源是指可以通过 URI (Uniform Resource Identifier) 访问的对象或数据集合。
  • 资源可以通过 HTTP 方法来操作。
    2. URI 的设计原则
  • URI 应该简洁明了。
  • 使用名词而不是动词来命名资源。
  • 使用复数形式来表示资源集合。
  • 使用路径参数来表示特定资源的实例。
    3. 示例:设计一个用户资源的 URI
  • 用户资源集合的 URI 可以设计为 /users
  • 特定用户的 URI 可以设计为 /users/{userId},其中 {userId} 是路径变量,代表用户的唯一标识符。

第二部分:Java 实现 RESTful API

在这个部分,我们将详细介绍如何使用 Java 和 Spring Boot 快速搭建 RESTful 服务,并通过具体的代码示例来展示如何创建 RESTful 控制器以及如何使用 JPA 进行数据持久化。

4. Spring Boot 快速搭建 RESTful 服务

Spring Boot 简介

Spring Boot 是 Spring 框架的一个衍生项目,旨在简化 Spring 应用的初始设置和配置。它提供了自动配置机制,使得开发者能够专注于编写业务逻辑代码,而不是繁琐的配置。

创建 Spring Boot 项目

为了创建一个新的 Spring Boot 项目,我们可以使用 Spring Initializr 来快速生成项目骨架。打开 Spring Initializr 网站 (https://start.spring.io/),选择以下选项:

  • Project: Maven Project
  • Language: Java
  • Packaging: Jar
  • Java: 11
  • Dependencies: Web, Thymeleaf (可选), Spring Data JPA, MySQL Driver (或您选择的数据库驱动)

点击“Generate”按钮下载项目模板。

添加 RESTful 支持

Spring Boot 默认已经包含了 RESTful 支持,我们不需要额外添加依赖。但是,为了方便测试 RESTful API,可以添加如下依赖:

<!-- pom.xml 文件 -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope>
</dependency>

启动类和配置文件示例

src/main/java 目录下创建 com.example.restapi 包,并在该包下创建 RestApiApplication.java 文件作为启动类:

package com.example.restapi;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class RestApiApplication {public static void main(String[] args) {SpringApplication.run(RestApiApplication.class, args);}
}

接着,在 src/main/resources 目录下的 application.properties 文件中添加数据库连接配置:

spring.datasource.url=jdbc:mysql://localhost:3306/rest_api?useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=password
spring.jpa.hibernate.ddl-auto=update

请确保替换数据库连接字符串、用户名和密码为您的实际配置。


5. 创建 RESTful 控制器

使用 @RestController 注解

@RestController 注解是一个组合注解,它结合了 @Controller@ResponseBody 注解的功能,用于标记一个类为 RESTful 控制器。

处理 HTTP 请求

下面是一个简单的例子,展示如何创建一个用户资源控制器:

package com.example.restapi.controller;import com.example.restapi.model.User;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;import java.util.ArrayList;
import java.util.List;
import java.util.Optional;@RestController
@RequestMapping("/api/users")
public class UserController {private List<User> users = new ArrayList<>();@PostMappingpublic ResponseEntity<User> createUser(@RequestBody User user) {users.add(user);return ResponseEntity.status(201).body(user);}@GetMapping("/{id}")public ResponseEntity<User> getUserById(@PathVariable Long id) {Optional<User> user = users.stream().filter(u -> u.getId().equals(id)).findFirst();return user.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build());}
}

返回 JSON 数据

在上面的例子中,我们使用了 ResponseEntity 类来控制响应的状态码和内容类型。默认情况下,Spring Boot 会将 Java 对象转换为 JSON 格式返回给客户端。


6. 使用 JPA 进行数据持久化

JPA 简介

Java Persistence API (JPA) 是 Java EE 规范的一部分,它提供了一个面向对象的方法来处理关系型数据库。Spring Data JPA 是 JPA 的一个简化版,它简化了数据访问层的编码。

创建实体类

首先,我们需要定义一个实体类来映射数据库表。例如,定义一个 User 类:

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.IDENTITY)private Long id;private String name;private String email;// Getters and setters
}

创建 Repository 接口

接着,我们创建一个 UserRepository 接口来处理数据访问:

package com.example.restapi.repository;import com.example.restapi.model.User;
import org.springframework.data.jpa.repository.JpaRepository;public interface UserRepository extends JpaRepository<User, Long> {User findByEmail(String email);
}

示例:实现 CRUD 操作

现在我们可以使用 UserRepository 来实现 CRUD (Create, Read, Update, Delete) 操作。修改 UserController 类来使用 UserRepository

package com.example.restapi.controller;import com.example.restapi.model.User;
import com.example.restapi.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;import java.util.List;
import java.util.Optional;@RestController
@RequestMapping("/api/users")
public class UserController {@Autowiredprivate UserRepository userRepository;@PostMappingpublic ResponseEntity<User> createUser(@RequestBody User user) {User createdUser = userRepository.save(user);return ResponseEntity.status(201).body(createdUser);}@GetMappingpublic ResponseEntity<List<User>> getAllUsers() {List<User> users = userRepository.findAll();return ResponseEntity.ok(users);}@GetMapping("/{id}")public ResponseEntity<User> getUserById(@PathVariable Long id) {Optional<User> user = userRepository.findById(id);return user.map(ResponseEntity::ok).orElseGet(() -> ResponseEntity.notFound().build());}@PutMapping("/{id}")public ResponseEntity<User> updateUser(@PathVariable Long id, @RequestBody User updatedUser) {return userRepository.findById(id).map(user -> {user.setName(updatedUser.getName());user.setEmail(updatedUser.getEmail());User savedUser = userRepository.save(user);return ResponseEntity.ok(savedUser);}).orElseGet(() -> ResponseEntity.notFound().build());}@DeleteMapping("/{id}")public ResponseEntity<?> deleteUser(@PathVariable Long id) {return userRepository.findById(id).map(user -> {userRepository.delete(user);return ResponseEntity.ok().build();}).orElseGet(() -> ResponseEntity.notFound().build());}
}

第三部分:高级主题

在这个部分,我们将深入探讨 RESTful API 的一些高级主题,包括认证与授权、异常处理以及如何测试 RESTful API。

7. 认证与授权

基本认证

基本认证是一种简单的身份验证机制,客户端通过 HTTP 头部发送用户名和密码。虽然简单易用,但它并不安全,不建议在生产环境中使用。

OAuth 2.0 简介

OAuth 2.0 是一种开放标准,用于授权应用程序访问受保护资源。它定义了客户端如何获取访问令牌,以及如何使用这些令牌来访问资源。

JWT (JSON Web Tokens)

JWT 是一种用于在各方之间安全地传输信息的标准格式。JWT 由三部分组成:头部、载荷和签名。JWT 通常用于认证过程,因为它可以包含用户的身份信息,并且可以在客户端和服务器之间传递。

示例:使用 Spring Security 配置 JWT

为了实现 JWT 认证,我们需要配置 Spring Security 并创建 JWT 相关的组件。

首先,在 pom.xml 中添加 Spring Security 依赖:

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId>
</dependency>

接着,创建一个配置类来配置 Spring Security:

package com.example.restapi.config;import com.example.restapi.security.JwtAuthenticationEntryPoint;
import com.example.restapi.security.JwtAuthenticationFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {@Autowiredprivate UserDetailsService userDetailsService;@Autowiredprivate JwtAuthenticationEntryPoint unauthorizedHandler;@Beanpublic JwtAuthenticationFilter authenticationJwtTokenFilter() {return new JwtAuthenticationFilter();}@Overridepublic void configure(AuthenticationManagerBuilder authenticationManagerBuilder) throws Exception {authenticationManagerBuilder.userDetailsService(userDetailsService).passwordEncoder(passwordEncoder());}@Bean@Overridepublic AuthenticationManager authenticationManagerBean() throws Exception {return super.authenticationManagerBean();}@Beanpublic PasswordEncoder passwordEncoder() {return new BCryptPasswordEncoder();}@Overrideprotected void configure(HttpSecurity httpSecurity) throws Exception {httpSecurity.cors().and().csrf().disable().exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and().sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and().authorizeRequests().antMatchers("/api/auth/**").permitAll().antMatchers("/api/test/**").permitAll().anyRequest().authenticated();httpSecurity.addFilterBefore(authenticationJwtTokenFilter(), UsernamePasswordAuthenticationFilter.class);}
}

还需要创建 JwtAuthenticationFilterJwtAuthenticationEntryPoint 类来处理 JWT 的认证过滤和未授权的请求。


8. 异常处理

自定义异常类

在 RESTful API 中,我们经常需要自定义异常类来处理特定的错误情况。例如,定义一个 BadRequestException

package com.example.restapi.exception;public class BadRequestException extends RuntimeException {public BadRequestException(String message) {super(message);}
}

全局异常处理器

为了统一处理异常,我们可以创建一个全局异常处理器来捕获并处理异常:

package com.example.restapi.exception;import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;import java.util.Date;@ControllerAdvice
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {@ExceptionHandler(value = {BadRequestException.class})public ResponseEntity<Object> handleBadRequest(BadRequestException ex, WebRequest request) {ErrorResponse error = new ErrorResponse(new Date(),ex.getMessage(),request.getDescription(false));return new ResponseEntity<>(error, HttpStatus.BAD_REQUEST);}@Overrideprotected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex,org.springframework.http.HttpHeaders headers,HttpStatus status,WebRequest request) {ErrorResponse error = new ErrorResponse(new Date(),"Validation Failed",ex.getBindingResult().getAllErrors().get(0).getDefaultMessage());return new ResponseEntity<>(error, HttpStatus.BAD_REQUEST);}
}

示例:处理无效请求

下面是一个处理无效请求的示例:

package com.example.restapi.controller;import com.example.restapi.exception.BadRequestException;
import com.example.restapi.model.User;
import com.example.restapi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;import javax.validation.Valid;@RestController
@RequestMapping("/api/users")
public class UserController {@Autowiredprivate UserService userService;@PostMappingpublic ResponseEntity<User> createUser(@Valid @RequestBody User user, BindingResult result) {if (result.hasErrors()) {throw new BadRequestException("Invalid user data");}return ResponseEntity.status(201).body(userService.createUser(user));}
}

9. 测试 RESTful API

单元测试

单元测试是为了确保每个函数或方法按预期工作。例如,测试 UserService 中的方法:

package com.example.restapi.service;import com.example.restapi.model.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;import static org.junit.jupiter.api.Assertions.assertNotNull;@SpringBootTest
public class UserServiceTests {@Autowiredprivate UserService userService;@Testpublic void testCreateUser() {User user = new User("John Doe", "johndoe@example.com");User createdUser = userService.createUser(user);assertNotNull(createdUser);}
}

集成测试

集成测试是为了确保多个组件一起工作时的行为符合预期。例如,使用 MockMvc 来模拟 HTTP 请求:

package com.example.restapi.controller;import com.example.restapi.model.User;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.test.web.servlet.MockMvc;import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;@WebMvcTest(UserController.class)
public class UserControllerTests {@Autowiredprivate MockMvc mockMvc;@Testpublic void shouldCreateUser() throws Exception {User user = new User("John Doe", "johndoe@example.com");mockMvc.perform(post("/api/users").contentType("application/json").content(objectMapper.writeValueAsString(user))).andExpect(status().isCreated());}
}

使用 MockMvc 进行模拟请求

在上面的测试示例中,我们使用了 MockMvc 来发送 POST 请求,并验证了返回的状态码是否为 201 Created


这篇关于【实战指南】RESTful 从入门到精通(Spring Boot)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1119374

相关文章

Spring Boot中WebSocket常用使用方法详解

《SpringBoot中WebSocket常用使用方法详解》本文从WebSocket的基础概念出发,详细介绍了SpringBoot集成WebSocket的步骤,并重点讲解了常用的使用方法,包括简单消... 目录一、WebSocket基础概念1.1 什么是WebSocket1.2 WebSocket与HTTP

SpringBoot+Docker+Graylog 如何让错误自动报警

《SpringBoot+Docker+Graylog如何让错误自动报警》SpringBoot默认使用SLF4J与Logback,支持多日志级别和配置方式,可输出到控制台、文件及远程服务器,集成ELK... 目录01 Spring Boot 默认日志框架解析02 Spring Boot 日志级别详解03 Sp

java中反射Reflection的4个作用详解

《java中反射Reflection的4个作用详解》反射Reflection是Java等编程语言中的一个重要特性,它允许程序在运行时进行自我检查和对内部成员(如字段、方法、类等)的操作,本文将详细介绍... 目录作用1、在运行时判断任意一个对象所属的类作用2、在运行时构造任意一个类的对象作用3、在运行时判断

java如何解压zip压缩包

《java如何解压zip压缩包》:本文主要介绍java如何解压zip压缩包问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Java解压zip压缩包实例代码结果如下总结java解压zip压缩包坐在旁边的小伙伴问我怎么用 java 将服务器上的压缩文件解压出来,

SpringBoot中SM2公钥加密、私钥解密的实现示例详解

《SpringBoot中SM2公钥加密、私钥解密的实现示例详解》本文介绍了如何在SpringBoot项目中实现SM2公钥加密和私钥解密的功能,通过使用Hutool库和BouncyCastle依赖,简化... 目录一、前言1、加密信息(示例)2、加密结果(示例)二、实现代码1、yml文件配置2、创建SM2工具

Spring WebFlux 与 WebClient 使用指南及最佳实践

《SpringWebFlux与WebClient使用指南及最佳实践》WebClient是SpringWebFlux模块提供的非阻塞、响应式HTTP客户端,基于ProjectReactor实现,... 目录Spring WebFlux 与 WebClient 使用指南1. WebClient 概述2. 核心依

Spring Boot @RestControllerAdvice全局异常处理最佳实践

《SpringBoot@RestControllerAdvice全局异常处理最佳实践》本文详解SpringBoot中通过@RestControllerAdvice实现全局异常处理,强调代码复用、统... 目录前言一、为什么要使用全局异常处理?二、核心注解解析1. @RestControllerAdvice2

Spring IoC 容器的使用详解(最新整理)

《SpringIoC容器的使用详解(最新整理)》文章介绍了Spring框架中的应用分层思想与IoC容器原理,通过分层解耦业务逻辑、数据访问等模块,IoC容器利用@Component注解管理Bean... 目录1. 应用分层2. IoC 的介绍3. IoC 容器的使用3.1. bean 的存储3.2. 方法注

Python设置Cookie永不超时的详细指南

《Python设置Cookie永不超时的详细指南》Cookie是一种存储在用户浏览器中的小型数据片段,用于记录用户的登录状态、偏好设置等信息,下面小编就来和大家详细讲讲Python如何设置Cookie... 目录一、Cookie的作用与重要性二、Cookie过期的原因三、实现Cookie永不超时的方法(一)

从入门到精通MySQL联合查询

《从入门到精通MySQL联合查询》:本文主要介绍从入门到精通MySQL联合查询,本文通过实例代码给大家介绍的非常详细,需要的朋友可以参考下... 目录摘要1. 多表联合查询时mysql内部原理2. 内连接3. 外连接4. 自连接5. 子查询6. 合并查询7. 插入查询结果摘要前面我们学习了数据库设计时要满