• 【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()

  • 相关阅读:
    可视化大屏报表的设计与制作 | 附成果图
    map中插入数据
    如何实现智能场景的搭建与升级?看看这家企业是怎么做的
    排列字母(蓝桥杯)
    realEngine(UE4)实现开关门效果
    HTTP 协议概述
    Execution failed for task ‘:app:javaPreCompileDebug‘.
    怎么批量将视频锐化处理并垂直翻转画面?
    小程序原生开发中的onLoad和onShow
    基于STM32G431嵌入式学习笔记——六、串口中断实例(基于第12届蓝桥杯串口部分题目)
  • 原文地址:https://blog.csdn.net/liuwanqing233333/article/details/126888106