解决Postman发送请求报错org.springframework.web.bind.MissingServletRequestParameterException

异常现象

最近使用Postman调试接口时出现报错如下:

org.springframework.web.bind.MissingServletRequestParameterException: Required String parameter 'userId' is not present
问题原因

首先从报错的字面信息来理解,是说有一个必填参数没有传值;
如果这个参数是非必填参数的话,我们应该设置该参数的required属性为false: @RequestParam(required = false)
但是我所设计的这个接口就是根据这个ID参数去查询数据,本身就是必填参数。

其次想到是否前端传递的参数名称、大小写和后端接口定义的参数不一致导致,对比后发现是一致的。

这时从后端接口上看出了问题所在。我后端接口参数用的是@RequestParam接收,而在Postman里我是用的application/json方式传递的请求数据,后端是无法接收到的。
rawjson

@RequestBody和@RequestParam区别
  • @RequestParam
    用来处理请求头为Content-Type: application/x-www-form-urlencoded编码的内容。
    Http协议中,默认传参类型就是application/x-www-form-urlencoded
    URL中拼接参数也会使用此类型传递。
  • @RequestBody
    处理HttpEntity传递过来的数据,一般用来处理非Content-Type: application/x-www-form-urlencoded编码格式的数据。
    我们前端传递的json对象一般都以此方式在后端接收。
解决办法

Postman里将传参方式改为form-data方式,或者将参数拼接在url后面。
fromdata

  • 8
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
org.springframework.web.bind.MissingServletRequestParameterException是Spring框架中的一个异常类,表示请求参数缺失的异常。 该异常通常会在处理HTTP请求时抛出,当请求中缺少必需的参数时,就会引发这个异常。例如,当请求中缺少名为"userId"的字符串类型参数时,就会抛出"Required String parameter 'userId' is not present"的异常信息。这个异常的原因是请求缺少了必需的参数。 在日志中,也可以找到对应异常的记录。比如在2021-04-06 18:29:13.820的日志中,出现了"org.springframework.web.bind.MissingServletRequestParameterException: Required Long parameter ‘enterpriseInfoId’ is not present"的异常信息。这表示在处理请求时,缺少了名为"enterpriseInfoId"的长整型参数。 综上所述,"org.springframework.web.bind.MissingServletRequestParameterException"是Spring框架中的一个异常,表示请求参数缺失的异常。通常情况下,这个异常会在处理HTTP请求时抛出,当请求中缺少必需的参数时,就会引发该异常。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [spring-web-2.5.jar](https://download.csdn.net/download/shenggaofei/10420498)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [解决Postman发送请求报错org.springframework.web.bind.MissingServletRequestParameterException](https://blog.csdn.net/u800820/article/details/122530951)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [org.springframework.web.bind.MissingServletRequestParameterException: Required Long parameter ...](https://blog.csdn.net/weixin_47056195/article/details/115496096)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值