springBoot--web--函数式web

函数式web

前言

springmvc5.2 以后允许我们使用函数式的方式,定义web的请求处理流程

函数式接口

web请求处理的方式:

1、@controller + @RequestMapping: 耦合性(路由、业务耦合)

2、函数式web:分离式(路由、业务分离)
官方文档

场景

场景:user Restful-crud

GET/user/1 获取1号用户

GET/users 获取所有用户

POST/user 请求体携带json

put/user/1 请求体携带json,修改1号用户

delete/user/1 删除1号用户

给容器中放一个Bean:类型是 RouterFunction

java 复制代码
package com.atguigu.boot304demo.config;

import com.atguigu.boot304demo.biz.UserBizHandler;
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;

/**
 * @author jitwxs
 * @date 2023年10月22日 21:33
 */
@Configuration
public class WebFunctionConfig {
    /*函数式web:
    1、给容器中放一个Bean:类型是 RouterFunction<ServerResponse>
    2、每个业务准备一个自己的handler


    核心四大对象:
    1、RouterFunction:定义路由信息,发什么请求,谁来处理
    2、RequestPredicate: 定义请求:请求谓语,请求方式(GET\POSt)、请求参数
    3、ServerTequest: 封装请求完整数据
    4、ServerResponse: 封装响应完整数据
    */
    @Bean
    public RouterFunction<ServerResponse> userRouter(UserBizHandler userBizHandler){
        return RouterFunctions.route()
                .GET("/user/{id}", RequestPredicates.accept(MediaType.ALL),userBizHandler::getUser)
                .GET("/users", userBizHandler::getUsers)
                .POST("/user",RequestPredicates.accept(MediaType.APPLICATION_JSON), userBizHandler::postUser)
                .PUT("/user/{id}",RequestPredicates.accept(MediaType.APPLICATION_JSON),userBizHandler::putUser)
                .DELETE("/user/{id}",userBizHandler::deleteUser)
                .build();
    }
}

每个业务准备一个自己的handler

java 复制代码
package com.atguigu.boot304demo.biz;

import com.atguigu.boot304demo.bean.Person;
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;
import java.util.List;

/**
 * @author jitwxs
 * @date 2023年10月22日 21:51
 */
@Slf4j
@Service
public class UserBizHandler {
    /*
    查询指定id的用户
    @param request
    @return
    */
    public ServerResponse getUser(ServerRequest request){
//        业务处理
        String id = request.pathVariable("id");
        log.info("正在查询id为{}的数据",id);
        Person person = new Person(2l,"张三","aaa.com",18);
        return ServerResponse
                .ok()
                .body(person);
    }

    public ServerResponse getUsers(ServerRequest request){
//        业务处理
        List<Person> list = Arrays.asList(
                new Person(1l,"张三","aaa.com",18),
                new Person(2l,"张三","aaa.com",18)
        );
        return ServerResponse
                .ok()
                .body(list);
    }

    public ServerResponse postUser(ServerRequest request) throws ServletException, IOException {
        Person body = request.body(Person.class);
        log.info("保存的信息是{}",body);
        String ace = "post请求成功";
//        业务处理
        return ServerResponse
                .ok()
                .body(ace);
    }

    public ServerResponse putUser(ServerRequest request){
        String ace = "put请求成功";
//        业务处理
        return ServerResponse
                .ok()
                .body(ace);
    }

    public ServerResponse deleteUser(ServerRequest request){
        String ace = "删除成功";
//        业务处理
        return ServerResponse
                .ok()
                .body(ace);
    }
}

使用集合的时候加注解

请求的效果


相关推荐
AI人工智能+电脑小能手几秒前
【大白话说Java面试题 第112题】【并发篇】第12题:AQS 中节点的入队时机有哪些?
java·开发语言·面试
摇滚侠1 分钟前
SpringMVC 入门到实战 处理静态资源的过程 64
java·后端·spring·maven·intellij-idea
影寂ldy2 分钟前
C# 泛型委托
java·算法·c#
程序猿阿伟2 分钟前
《Chrome标签组搭建多任务高效浏览指南》
前端·chrome
摇滚侠3 分钟前
MyBatis 入门到项目实战 MyBatis 核心配置文件 15-19
java·tomcat·mybatis
IT WorryFree3 分钟前
Zabbix 7.4 API 可同步全量参数清单(同步第三方系统专用)
java·开发语言·zabbix
RoboWizard5 分钟前
一块硬盘上架前要闯多少关?
java·服务器·数据库
2601_9583529013 分钟前
双麦 DSP 音频模块实战:一文梳理 A-68 在全行业场景的声学解决方案与落地要点
前端·嵌入式硬件·音视频·语音识别·降噪消回音·音频处理模块
Liquad Li15 分钟前
ABP vNext 标准分层解决方案项目结构完整解析
后端
半夜燃烧的香烟16 分钟前
docker 安装minio nginx,配置nginx根据文根路由minio展示图片
java·nginx·docker