SpringBoot2之PUT请求接收不了参数的解决方案

作者:Moshow郑锴 时间:2023-08-23 01:32:07 

SpringBoot2之PUT请求接收不了参数的解决办法,这个问题,关乎两个Filter过滤器,是spring3和3.5之后提供的,目的就是解决RESTful中PUT请求或者其他请求的问题。

下面请看详细内容

HiddenHttpMethodFilter

SpringBoot2之PUT请求接收不了参数的解决方案

html中form表单只支持GET与POST请求,而DELETE、PUT等method并不支持,spring3添加了一个过滤器,可以将这些请求转换为标准的http方法,使得支持GET、POST、PUT与DELETE请求。

@Bean
   public FilterRegistrationBean<HiddenHttpMethodFilter> testFilterRegistration3() {
       FilterRegistrationBean<HiddenHttpMethodFilter> registration = new FilterRegistrationBean<HiddenHttpMethodFilter>();
       registration.setFilter(new HiddenHttpMethodFilter());//添加过滤器
       registration.addUrlPatterns("/*");//设置过滤路径,/*所有路径
       registration.setName("HiddenHttpMethodFilter");//设置优先级
       registration.setOrder(2);//设置优先级
       return registration;
   }

在页面的form表单中设置method为Post,并添加一个如下的隐藏域:

<input type="hidden" name="_method" value="put" />

查看HiddenHttpMethodFilter源码

String paramValue = request.getParameter(methodParam);  
       if("POST".equals(request.getMethod()) && StringUtils.hasLength(paramValue)) {  
           String method = paramValue.toUpperCase(Locale.ENGLISH);  
           HttpServletRequest wrapper = new HttpMethodRequestWrapper(request, method);
           filterChain.doFilter(wrapper, response);  
       } else  
       {  
           filterChain.doFilter(request, response);  
       }  
}

 由源码可以看出,filter只对Post方法进行过滤,且需要添加参数名为_method的隐藏域,也可以设置其他参数名,比如想设置为_method_,可以在HiddenHttpMethodFilter配置类中设置初始化参数:put (methodParam,"_method_") 

HttpPutFormContentFilter

SpringBoot2之PUT请求接收不了参数的解决方案

由HiddenHttpMethodFilter可知,html中的form的method值只能为post或get,我们可以通过HiddenHttpMethodFilter获取put表单中的参数键值对,而在Spring3中获取put表单的参数键值对还有另一种方法,即使用HttpPutFormContentFilter过滤器。

@Bean
   public FilterRegistrationBean<HttpPutFormContentFilter> testFilterRegistration2() {
       FilterRegistrationBean<HttpPutFormContentFilter> registration = new FilterRegistrationBean<HttpPutFormContentFilter>();
       registration.setFilter(new HttpPutFormContentFilter());//添加过滤器
       registration.addUrlPatterns("/*");//设置过滤路径,/*所有路径
       registration.setName("HttpPutFormContentFilter");//设置优先级
       registration.setOrder(2);//设置优先级
       return registration;
   }

HttpPutFormContentFilter过滤器的作为就是获取put表单的值,并将之传递到Controller中标注了method为RequestMethod.put的方法中。

与HiddenHttpMethodFilter不同,在form中不用添加参数名为_method的隐藏域,且method不必是post,直接写成put,但该过滤器只能接受enctype值为application/x-www-form-urlencoded的表单,也就是说,在使用该过滤器时,form表单的代码必须如下:

<form action="" method="put" enctype="application/x-www-form-urlencoded">  
   ......  
</form>

另外,经过测试,json数据也是ok的,enctype=&rdquo;application/json&rdquo;也是ok的

来源:https://zhengkai.blog.csdn.net/article/details/80173817

标签:SpringBoot2,PUT请求,接收,参数
0
投稿

猜你喜欢

  • Android实现GPS定位代码实例

    2022-07-14 17:26:21
  • 实例化JFileChooser对象报空指针异常问题的解决办法

    2023-10-05 11:36:18
  • Spring Boot 单元测试JUnit的实践

    2023-08-29 02:02:15
  • 浅谈Java中Map和Set之间的关系(及Map.Entry)

    2023-08-25 02:23:48
  • C#开发的人脸左右相似度计算软件源码分析

    2023-08-26 05:18:41
  • C++中左值和右值的区别详解

    2021-09-11 00:07:42
  • C# 游戏外挂实现核心代码

    2021-12-28 14:47:40
  • C#发送邮箱实现代码

    2022-04-16 11:57:57
  • java jdbc连接和使用详细介绍

    2023-01-13 18:12:52
  • 浅谈一下Spring中的createBean

    2023-06-08 22:01:26
  • Java调取创蓝253短信验证码的实现代码

    2021-11-05 00:48:10
  • c#中XML解析文件出错解决方法

    2022-01-21 00:38:50
  • 使用SpringBoot获取resources文件路径

    2022-10-12 15:29:56
  • 一篇文章带你深入了解Java封装

    2023-11-20 00:37:45
  • 基于Spring Boot使用JpaRepository删除数据时的注意事项

    2023-04-03 09:05:39
  • Java中的异常处理(try,catch,finally,throw,throws)

    2021-07-29 16:45:12
  • 全面总结java IO体系

    2023-05-16 13:19:12
  • Android图片处理实例分析

    2022-09-10 20:34:50
  • java如何连续执行多条cmd命令

    2023-07-13 13:10:41
  • Android滑动事件冲突详解(一)

    2022-07-22 18:20:22
  • asp之家 软件编程 m.aspxhome.com