相关文章推荐
追风的铁链  ·  WebServer应用示例:不到100行代码 ...·  1 年前    · 
酷酷的金针菇  ·  《C语言程序设计》实验教学内容-智能制造学部·  1 年前    · 
坚强的面包  ·  How to do a dynamic ...·  2 年前    · 
千杯不醉的牙膏  ·  Js设置打印缩放 - 旧巷里的旧少年 - 博客园·  2 年前    · 
不拘小节的马克杯  ·  Bash foreach loop - ...·  2 年前    · 
Code  ›  http - HTTP PUT请求该如何传输请求参数呢? -
curl http请求
https://segmentfault.com/q/1010000006665586
千杯不醉的上铺
1 年前
segmentfault segmentfault
注册登录
问答 博客 活动
发现
✓ 使用“Bing”搜本站 使用“Google”搜本站 使用“百度”搜本站 站内搜索
注册登录
  1. 首页
  2. 问答
  3. http
  4. 问答详情

HTTP PUT请求该如何传输请求参数呢?

头像
zhuguowei2
825 18 136 189
发布于
2016-08-20
更新于
2016-08-20

PUT请求该如何传输请求参数呢?

有如下的接口

@RequestMapping(value = "testPut", method = RequestMethod.PUT)
public Result<Void> testPut(@RequestParam String foo, @RequestParam String bar) {
    System.out.println(foo + " " + bar) ;
    return Result.success(null);

通过CURL来调用该接口

# 通过-d传输请求参数失败
curl -X PUT  -d "foo=foo&bar=bar" 'http://localhost:8080/testPut'
org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'foo' is not present
# 但直接放在url后面是可以的
curl -X PUT  'http://localhost:8080/testPut?foo=foo&bar=bar'

但我想像POST一样 以请求体的方式传输请求参数, 而不是作为URL的一部分

现在我的方法是

@RequestMapping(value = "testPut2", method = RequestMethod.PUT)
public Result<Void> testPut2(@RequestBody FooBar fooBar) {
    System.out.println(fooBar) ;
    return Result.success(null);
@Data
static class FooBar{
    public String foo;
    public String bar;

然后以json的方式调用

curl -X PUT  -H "Content-Type: application/json" -d '{"foo":"foo","bar":"bar"}' 'http://localhost:8080/testPut2'

这样是可以的 但奇怪为什么PUT只能接收json呢?

put http
阅读 78.2k
3 个回答
得票 最新
头像
_never
31 1 3 7
发布于
2017-07-04
更新于
2017-07-04

对于表单提交,tomcat默认只解析POST的表单,对于PUT和DELETE的不处理,所以Spring拿不到。
解决方案:1、修改tomcat的server.xml:

<Connector port="8080" protocol="HTTP/1.1" 
           connectionTimeout="20000"
           redirectPort="8443"
           parseBodyMethods="POST,PUT,DELETE"
           URIEncoding="UTF-8" />

解决方案2、在web.xml中添加 HttpPutFormContentFilter

    <!--Servlet不支持PUT表单,需要Spring支持-->
    <filter>
        <filter-name>httpPutFormContentFilter</filter-name>
        <filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>httpPutFormContentFilter</filter-name>
        <url-pattern>/*</url-pattern>
 
推荐文章
追风的铁链  ·  WebServer应用示例:不到100行代码玩转Siri语音控制 | ESP32轻松学(Arduino版)_esp32 语音转文字 arduino-CSDN博客
1 年前
酷酷的金针菇  ·  《C语言程序设计》实验教学内容-智能制造学部
1 年前
坚强的面包  ·  How to do a dynamic linq order by - CodeProject
2 年前
千杯不醉的牙膏  ·  Js设置打印缩放 - 旧巷里的旧少年 - 博客园
2 年前
不拘小节的马克杯  ·  Bash foreach loop - Stack Overflow
2 年前
今天看啥   ·   Py中国   ·   codingpro   ·   小百科   ·   link之家   ·   卧龙AI搜索
删除内容请联系邮箱 2879853325@qq.com
Code - 代码工具平台
© 2024 ~ 沪ICP备11025650号