(三)springboot实战——web新特性之函数式实现

news/2025/1/18 13:11:20/

前言

本节内容我们主要介绍一下web访问的另一种形式,通过函数式web实现一个restful风格的http请求案例。函数式web是spring5.2之后的一个新特性,可以通过函数去定义web请求的处理流程,使得代码更为简洁,耦合性也降低了。

正文

  • 函数式web的四大核心对象

- RouterFunction:定义路由信息

- RequestPredicates:定义请求规则,包括请求参数、请求方式等

- ServerRequest:封装请求参数

- ServerResponse:封装响应参数

  •  定义一个函数式web的路由组件WebFunctionConfig配置类

- 实现代码

package com.yundi.isbc.config;import com.yundi.isbc.handler.PersonHandlerService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.servlet.function.RequestPredicates;
import org.springframework.web.servlet.function.RouterFunction;
import org.springframework.web.servlet.function.RouterFunctions;
import org.springframework.web.servlet.function.ServerResponse;@Configuration
public class WebFunctionConfig {/*** 用户信息管理** @param personHandlerService* @return*/@Beanpublic RouterFunction<ServerResponse> personRouter(PersonHandlerService personHandlerService) {RouterFunction<ServerResponse> personRouterFunction = RouterFunctions.route().GET("/person/{id}", RequestPredicates.accept(MediaType.ALL), personHandlerService::getPerson).GET("/persons", personHandlerService::getPersons).POST("/person", RequestPredicates.accept(MediaType.APPLICATION_JSON), personHandlerService::savePerson).PUT("/person", RequestPredicates.accept(MediaType.APPLICATION_JSON), personHandlerService::updatePerson).DELETE("/person/{id}", personHandlerService::deletePerson).build();return personRouterFunction;}}

- 在该类中实现了restful风格的请求完成用户管理功能的开发

  • 用户person对象的实体类

- 用户Person类

package com.yundi.isbc.entity;import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import lombok.Data;
import lombok.experimental.Accessors;import java.util.ArrayList;
import java.util.List;@Accessors(chain = true)
@Data
public class Person {/*** id*/private String id;/*** 名称*/private String name;/*** 年龄*/private Integer age;/*** 手机号*/private String phone;/*** 住址*/private List<Address> addresses = new ArrayList<>();
}

- 用户地址类

package com.yundi.isbc.entity;import lombok.Data;
import lombok.experimental.Accessors;@Accessors(chain = true)
@Data
public class Address {/*** 地址名称*/private String addressName;
}
  • 定义业务实现接口PersonHandlerService 

- 实现代码

package com.yundi.isbc.handler;import jakarta.servlet.ServletException;
import org.springframework.web.servlet.function.ServerRequest;
import org.springframework.web.servlet.function.ServerResponse;import java.io.IOException;public interface PersonHandlerService {/*** 根据id获取用户信息** @param serverRequest* @return*/ServerResponse getPerson(ServerRequest serverRequest);/*** 获取全部用户* @param serverRequest* @return*/ServerResponse getPersons(ServerRequest serverRequest);/*** 保存用户* @param serverRequest* @return*/ServerResponse savePerson(ServerRequest serverRequest) throws ServletException, IOException;/*** 更新用户信息* @param serverRequest* @return*/ServerResponse updatePerson(ServerRequest serverRequest) throws ServletException, IOException;/*** 删除用户* @param serverRequest* @return*/ServerResponse deletePerson(ServerRequest serverRequest);
}

- 定义各功能业务实现接口方法

  • 定义业务实现类PersonHandlerServiceImpl

- 实现代码

package com.yundi.isbc.handler.impl;import com.yundi.isbc.entity.Address;
import com.yundi.isbc.entity.Person;
import com.yundi.isbc.handler.PersonHandlerService;
import jakarta.servlet.ServletException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.function.ServerRequest;
import org.springframework.web.servlet.function.ServerResponse;import java.io.IOException;
import java.util.Arrays;@Slf4j
@Service
public class PersonHandlerServiceImpl implements PersonHandlerService {@Overridepublic ServerResponse getPerson(ServerRequest serverRequest) {log.info("==================获取用户信息==================");String id = serverRequest.pathVariable("id");log.info("用户id:{}", id);return ServerResponse.ok().body(new Person().setId("1").setName("xiaoming").setAge(18).setPhone("18288474612").setAddresses(Arrays.asList(new Address().setAddressName("北京市朝阳区"))));}@Overridepublic ServerResponse getPersons(ServerRequest serverRequest) {log.info("==================获取全部用户信息==================");return ServerResponse.ok().body(Arrays.asList(new Person().setId("1").setName("xiaoming").setAge(18).setPhone("18288474612").setAddresses(Arrays.asList(new Address().setAddressName("北京市朝阳区")))));}@Overridepublic ServerResponse savePerson(ServerRequest serverRequest) throws ServletException, IOException {log.info("==================保存用户信息==================");Person person = serverRequest.body(Person.class);log.info("用户信息:{}", person);return ServerResponse.ok().build();}@Overridepublic ServerResponse updatePerson(ServerRequest serverRequest) throws ServletException, IOException {log.info("==================更新用户信息==================");Person person = serverRequest.body(Person.class);log.info("用户信息:{}", person);return ServerResponse.ok().build();}@Overridepublic ServerResponse deletePerson(ServerRequest serverRequest) {log.info("==================删除用户信息==================");String id = serverRequest.pathVariable("id");log.info("用户id:{}", id);return ServerResponse.ok().build();}
}

- 具体业务实现

  • 启动项目访问测试

 - 获取用户信息

- 获取用户全部信息

-  保存用户信息

 - 更新用户信息

 - 删除用户信息

结语

关于web新特性之函数式实现的相关内容到这里就结束了,我们下期见。。。。。。


http://www.ppmy.cn/news/972096.html

相关文章

[MySQL]MySQL事务特性

[MySQL]MySQL事务 文章目录 [MySQL]MySQL事务1. 事务的概念2. 为什么会出现事务3. 事务的版本支持4. 事务的提交方式5. 事务常见操作方式6. 事务隔离级别事务隔离级别概念查看事务的隔离级别设置事务的隔离级别读未提交&#xff08;Read Uncommitted&#xff09;读提交&#xf…

Linux Misc 驱动-编写驱动例程基于iTOP-STM32P157开发板

首先我们回想一下注册杂项设备的三大流程&#xff0c;我们在 Windows 上面新建 misc.c 文件&#xff0c;并用 sourceinsight 打开。我们可以将上次编写的 helloworld.c 里面的代码拷贝到 misc.c 文件&#xff0c;并修改为如下图所示 添加头文件 /*注册杂项设备头文件*/ #inc…

5 | Xpath

当进行网页爬取时,有多种方法可以从HTML文档中提取所需信息。其中一种常用的方法是使用XPath。XPath是一种用于在XML和HTML文档中定位元素的查询语言。在Python中,我们可以使用lxml库来实现XPath查询。本教程将教您如何使用Python中的lxml库和XPath来解析HTML文档,并提取所需…

基于半监督算法的工业图像缺陷检测方法:MemSeg

来源&#xff1a;投稿 作者&#xff1a;橡皮 编辑&#xff1a;学姐 论文&#xff1a;https://arxiv.org/ftp/arxiv/papers/2205/2205.00908.pdf 代码&#xff1a;https://github.com/TooTouch/MemSeg 主要贡献 提出了一个精心设计的异常模拟策略&#xff0c;用于模型的自监督…

C++---string

String C语言中的字符串和C中的string类标准库中的string类string类的常用接口string类对象的常见构造string类对象的容量操作string类对象的访问及遍历操作 C语言中的字符串和C中的string类 在C语言中&#xff0c;字符串是一个字符数组&#xff0c;它以空字符\0结尾&#xff…

苹果今年或无法推出M3芯片;​微软将推私有版ChatGPT:价格是常规版10倍;sudo和su用Rust重写|极客头条

「极客头条」—— 技术人员的新闻圈&#xff01; CSDN 的读者朋友们早上好哇&#xff0c;「极客头条」来啦&#xff0c;快来看今天都有哪些值得我们技术人关注的重要新闻吧。 整理 | 梦依丹 出品 | CSDN&#xff08;ID&#xff1a;CSDNnews&#xff09; 一分钟速览新闻点&…

苹果今年或无法推出M3芯片;​微软将推私有版ChatGPT:价格是常规版10倍;sudo和su用Rust重写|极客头条...

「极客头条」—— 技术人员的新闻圈&#xff01; CSDN 的读者朋友们早上好哇&#xff0c;「极客头条」来啦&#xff0c;快来看今天都有哪些值得我们技术人关注的重要新闻吧。 整理 | 梦依丹 出品 | CSDN&#xff08;ID&#xff1a;CSDNnews&#xff09; 一分钟速览新闻点&#…

强大的编辑器 Cursor,免费集成了 ChatGPT-4

AI 时代来临&#xff0c;还不会利用工具帮助你工作&#xff0c;很容易就会被淘汰了。 就在上一周是 AI&#xff0c;集体亮相的一周&#xff0c;ChatGPT-4、new-bing、文心一言、Claude 等等&#xff0c;一一体验过这些工具&#xff0c;能明显感觉到作为 CV 工程师的焦虑。 在…