Spring的RequestMethod.PUT方法接收不到参数

本文探讨了在Spring MVC框架下使用PUT方法时遇到的参数接收问题,并提供了两种解决方案:一种是在web.xml中配置HttpPutFormContentFilter过滤器;另一种是通过查阅Stack Overflow和Spring官方文档获得的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近的工作中遇到一个问题,在使用Spring时,RequestMethod.POST方法能够接收到参数,但RequestMethod.PUT却接收不到传进来的参数,代码大致如下:

@Controller
@RequestMapping("/v1")
public class AccountController {
    @RequestMapping(value = "/account/{id}", method = RequestMethod.PUT)
    @ResponseBody
    public Object updateUserInfo(@PathVariable Long id,
                                 @RequestParam(value = "phone", required = false) String phone,
                                 @RequestParam(value = "nickName", required = false) String userName) {
        // do something....
    }
}

id的参数是能收到,其他的不行。在网上搜索资料找到解决方法:

stackoverflow的原文地址:http://stackoverflow.com/questions/5894270/springmvc-is-not-recognizing-request-body-parameters-if-using-put

说是Servlet API的一些问题:https://jira.spring.io/browse/SPR-7414

有意思的是这个bug在spring的 Resolution是Won't Fix。

我目前的解决方法是在web.xml中加入:

<filter>
    <filter-name>httpPutFormContentFilter</filter-name>
    <filter-class>org.springframework.web.filter.HttpPutFormContentFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>httpPutFormContentFilter</filter-name>
    <servlet-name>rest</servlet-name>
</filter-mapping>

可以接收到数据了,另一种方法没测试,不知是否可行。

转载于:https://my.oschina.net/shellj/blog/392470

package com.example.demo; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.client.RestTemplate; // 引用海康安全认证库 import com.hikvision.artemis.sdk.ArtemisHttpUtil; import java.util.HashMap; import java.util.Map; @SpringBootApplication public class DemoApplication implements CommandLineRunner { private static final String API_URL = "http://192.25.10.58:443/api/acs/v2/door/events"; public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } @Override public void run(String... args) throws Exception { RestTemplate restTemplate = new RestTemplate(); // 构造请求参数 Map<String, Object> request = new HashMap<>(); request.put("pageNo", 1); request.put("pageSize", 10); request.put("doorIndexCodes", new String[]{"1f276203e5234bdca08f7d99e1097bba"}); request.put("doorName", "11"); request.put("readerDevIndexCodes", new String[]{"1f2762v4523547374"}); request.put("startTime", "2018-05-21T12:00:00+08:00"); request.put("endTime", "2018-05-21T12:00:00+08:00"); request.put("receiveStartTime", "2018-05-21T12:00:00+08:00"); request.put("receiveEndTime", "2018-05-21T12:00:00+08:00"); request.put("doorRegionIndexCode", "c654234f-61d4-4dcd-9d21-e7a45e0f1334"); request.put("eventTypes", new Integer[]{10}); request.put("personName", "xx"); request.put("sort", "personName"); request.put("order", "asc"); // 发送POST请求 String result = ArtemisHttpUtil.doPostStringArtemis(path, , request, "application/json"); // 打印返回结果 System.out.println("返回结果:"+response); } }重新完善代码
最新发布
03-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值