org.springframework.web.bind.MissingServletRequestParameterException
Posted 赵雯的技术篮子
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了org.springframework.web.bind.MissingServletRequestParameterException相关的知识,希望对你有一定的参考价值。
问题发生
对接口进行测试时。
情况介绍/分析
已实现一个接口:
package com.ybqdren.controller.center;
import com.ybqdren.pojo.Users;
import com.ybqdren.service.center.CenterUserService;
import com.ybqdren.utils.IMOOCJSONResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* Wen(Joan) Zhao <withzhaowen@126.com>
* 2021/11/9
*/
@Api(value = "center - 用户中心",tags = {"用户中心展示的相关接口"})
@RestController
@RequestMapping("center")
public class CenterController {
@Autowired
private CenterUserService centerUserService;
@ApiOperation(value = "获取用户信息",notes = "获取用户信息",httpMethod = "GET")
@GetMapping("userinfo")
public IMOOCJSONResult userinfo(
@ApiParam(name = "userId",value="用户id",required = true)
@RequestParam String userid
){
Users user = centerUserService.queryUserInfo(userid);
return IMOOCJSONResult.ok(user);
}
}
此处我们使用请求参数注解RequestParam,用来获取:
http://localhost:8080/center/userinfo/userid=xxx
这种形式的请求参数。
由于此处我们没有在RequestParam注解中指定参数的名称,所以SpringBoot会默认去url寻找一个叫userid的参数。
问题解决
问题定位
根据错误中的:
MissingServletRequestParameterException
可知是参数传递的问题。
再看:
Required request parameter \'userid\' for method parameter type String is not present]
会发现,一个叫做userid的参数出现了问题。
检查前端传入的参数名称:
serverUrl + \'/center/userInfo?userId=\' + userInfo.id,
回顾情况介绍/分析中,我们并没有指定RequestParam注解接受的参数名称,所以其在url中寻找的是一个叫做userid的参数,故而没找到。
解决方案
因此我们可以用下面三种方式进行解决:
- 修改前端传入过来的参数名称(学习环境可以用,但是最不推荐使用)
- 修改当前路由方法参数名称
- 在RequestParam注解中设置name的值(最好的一种方法)
本文来自博客园,作者:赵雯_后端开发工程师,转载请注明原文链接:https://www.cnblogs.com/ybqdren/p/15527112.html
Spring--Spring数据绑定
Spring数据绑定使用场景
- Spring BeanDefinition到Bean实例创建
- Spring数据绑定
- SpringWeb参数绑定
Spring数据绑定组件
- 标准组件: org.springframework.validation.DataBinder
- Web组件
1.org.springframework.web.bind.WebDataBinder
2.org.springframework.web.bindServletRequestDataBinder
3.org.springframework.web.bind.support.WebRequestDataBinder
4.org.springframework.web.bind.support.WebExchangeDataBinder
Spring数据绑定组件
- DataBinder核心属性
- DataBinder绑定方法
bind(PropertyValues):将PropertyValues Key-Value内容映射到关联Bean中的属性上
- Spring数据绑定元数据–PropertyValues
- Spring数据绑定控制参数
数据绑定支持配置忽略未知字段,配置自动增加嵌套路径等
BeanWrapper
BeanWrapper 是Spring提供的一个用来操作javaBean属性的工具,使用它可以直接修改一个对象的属性。
- BeanWrapper使用场景
1.Spring底层JavaBeans基础设施的中心化接口
2.通常不会直接使用,间接用于BeanFactory和DataBinder
3.提供标准JavaBeans分析和操作,能够单独或批量存储JavaBean的属性
4.支持嵌套属性路径
5.实现类org.springframework.beans.BeanWrapperImpl
标准JavaBeans是如何操作属性的
以上是关于org.springframework.web.bind.MissingServletRequestParameterException的主要内容,如果未能解决你的问题,请参考以下文章