• 【SpringBoot】请求参数处理 —— Rest使用与原理


    前言

    Rest 方式可以发送 GET 、POST、PUT、DELETE 等方式的请求。但是表单只能GET提交或POST提交,通过 HiddenHttpMethodFilter 拦截器可以处理PUT、DELETE请求。本文主要介绍拦截器的使用。
    其他的客户端工具,可以直接发送各种请求方式,因此无需filter做转换。因此,filter功能是选择性开启,不是一定要开启的。

    一、Controller类
    package com.example.demo.controller;
    
    import org.springframework.web.bind.annotation.DeleteMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestMethod;
    import org.springframework.web.bind.annotation.RestController;
    
    @RestController
    public class HelloCotroller {
    
        @RequestMapping(value = "/user",method = RequestMethod.GET)
        public String getUser(){
            return "GET-张三";
        }
    
        @RequestMapping(value = "/user",method = RequestMethod.POST)
        public String saveUser(){
            return "POST-张三";
        }
    
    
        @RequestMapping(value = "/user",method = RequestMethod.PUT)
        public String putUser(){
            return "PUT-张三";
        }
    
        // @RequestMapping(value = "/user",method = RequestMethod.DELETE)
        @DeleteMapping(value = "/user")
        public String deleteUser(){
            return "DELETE-张三";
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    1. Delete请求两种写法——其他请求同理
    @DeleteMapping(value = "/user")
    
    • 1
    @RequestMapping(value = "/user",method = RequestMethod.DELETE)
    
    • 1
    二、配置文件
    spring:
      mvc:
        hiddenmethod:
          filter:
            enabled: true #开启表单的filter功能
    
    • 1
    • 2
    • 3
    • 4
    • 5
    三、请求写法

    在post请求中加入

    <input type="hidden" name="_method" value="delete">
    
    • 1

    完整代码:

    
    <form action="/user" method="post">
        
        <input type="hidden" name="_method" value="delete">
        <input type="submit" value="REST-DELETE 提交">
    form>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    四、自定义 _method 名字
    1. 自定义Filter
      创建一config包 —— 在config包下定义WebMvcConfigurer类
    //自定义filter
    package com.example.demo.config;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.filter.HiddenHttpMethodFilter;
    
    @Configuration(proxyBeanMethods = false)
    public class WebConfig implements WebMvcConfigurer {
    
        @Bean
        public HiddenHttpMethodFilter hiddenHttpMethodFilter(){
            HiddenHttpMethodFilter methodFilter = new HiddenHttpMethodFilter();
            methodFilter.setMethodParam("_myMethod");
            return methodFilter;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    Filter原理

    ● 表单提交会带上_method=PUT (大小写都可以)
    ● 请求被HiddenHttpMethodFilter拦截
    1. 判断请求是否正常,并且是POST类请求
    ■ 获取到_method的值(value)
    ■ 兼容以下请求;PUT.DELETE.PATCH
    ■ 原生request(post),包装模式requesWrapper重写了getMethod方法,返回的是传入的值
    ■ 过滤器链放行的时候用wrapper。以后的方法调用getMethod是调用requesWrapper的。


    请求映射原理
    源码:
    org.springframework.web.servlet.DispatcherServlet —— doDispatch()

  • 相关阅读:
    Linux时间指令
    rerank来提升RAG的准确度的策略
    rknn3588 rstp yolov5
    Linux 内核活动专题
    ReLU激活函数
    Java实验报告(一)
    存储器管理
    前端 JS 经典:i,i++,++i区别
    Linux网络编程系列之UDP广播
    OS2.3.7:多生产者,多消费者问题
  • 原文地址:https://blog.csdn.net/liuwanqing233333/article/details/126888106