前言
本节内容我们主要介绍一下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新特性之函数式实现的相关内容到这里就结束了,我们下期见。。。。。。