说明:函数式web是spring5.2之后的一个新特性,Spring Boot 3 进一步优化了这一模型,为开发现代 Web 应用提供了更加灵活、简洁的方法;
- 函数式web的四大核心对象
- RouterFunction:定义路由信息
- RequestPredicates:定义请求规则,包括请求参数、请求方式等
- ServerRequest:封装请求参数
- ServerResponse:封装响应参数
一、引包
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.0.0</version>
</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-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
二、routerFunction配置类
import com.zc.handler.HostDiffHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.servlet.function.*;
/**
* @author zc
* @date 2024/4/15 19:02
* @desc
*/
@Configuration
public class WebFunctionConfig {
/**
* 函数式编程
* @param hostDiffHandler
* @return
*/
@Bean
public RouterFunction<ServerResponse> HostDiffRouter(HostDiffHandler hostDiffHandler){
return RouterFunctions.route().
GET("hostDiff/{id}", RequestPredicates.accept(MediaType.APPLICATION_JSON), hostDiffHandler::getHostDiff).
POST("hostDiff/add", RequestPredicates.accept(MediaType.APPLICATION_JSON), hostDiffHandler::addHostDiff).
PATCH("hostDiff/update", RequestPredicates.accept(MediaType.APPLICATION_JSON), hostDiffHandler::updateHostDiff).
DELETE("hostDiff/{id}", hostDiffHandler::delHostDiff).
build();
}
}
三、handler 处理类
说明: 这里的TestHostService 实现的是mybatis-plus 的IService 方式,请参考:Mybatis plus 实现IService方式,使用SpringbootTest引入service 测试_mybatisplus的iservice方法-CSDN博客
import com.zc.bean.HostDiffBean;
import com.zc.service.TestHostService;
import jakarta.servlet.ServletException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.servlet.function.ServerRequest;
import org.springframework.web.servlet.function.ServerResponse;
import java.io.IOException;
/**
* @author zc
* @date 2024/4/15 19:04
* @desc
*/
@Service
@Slf4j
public class HostDiffHandler {
@Autowired
private TestHostService testHostService;
/**
* 获取
* @param serverRequest
* @return
*/
public ServerResponse getHostDiff(ServerRequest serverRequest){
String id = serverRequest.pathVariable("id");
return ServerResponse.ok().body(testHostService.getById(id));
}
/**
* 新增
* @param serverRequest
* @return
*/
public ServerResponse addHostDiff(ServerRequest serverRequest) throws ServletException, IOException {
testHostService.save(serverRequest.body(HostDiffBean.class));
return ServerResponse.ok().build();
}
/**
* 更新
* @param serverRequest
* @return
*/
public ServerResponse updateHostDiff(ServerRequest serverRequest) throws ServletException, IOException {
testHostService.updateById(serverRequest.body(HostDiffBean.class));
return ServerResponse.ok().build();
}
/**
* 删除
* @param serverRequest
* @return
*/
public ServerResponse delHostDiff(ServerRequest serverRequest){
testHostService.removeById(serverRequest.pathVariable("id"));
return ServerResponse.ok().build();
}
}
四、测试
五、参考:
重学SpringBoot3-函数式Web-CSDN博客
(三)springboot实战——web新特性之函数式实现_springboot function-CSDN博客