侧边栏

springmvd接收参数问题

问题描述:

好久不写博客了,今天遇到一个问题,那就是post请求时,参数接收不到,当时我很纳闷,看代码:

就是这样几个参数,我使用postman请求时无法获取参数:

 

报错信息:

"msg":"Optional int parameter 'tableId' is present but cannot be translated into a null value due to

后来上网查了相关资料:

接口测试的时候遇到了一个问题,导致测试阻断了好久,在此记录,谨防忘记。
具体报错如下:
Optional int parameter 'pId' is present but cannot be translated into a null value due to being declared as a primitive type. Consider declaring it as object wrapper for the corresponding primitive type.
归根结底就是参数类型错误了:
可选的参数 pId不存在,但无法被转换为NULL,是因为你把它给定义为 基本类型。建议将其修改为 包装类型。
就是说,你定义了参数:String pId,但没有值,那按理来说按照null来处理,结果倒霉的事情来了:pId= null; 是不允许的,因为基础类型不能赋值为null。
所以建议把参数定义修改为Inteter pId.
那为啥用Integer可以,用int不行呢,原因如下:
Integer 允许为null值,int默认0,数据库里面如果有个字段没有值可能默认值为null,所以用Integer。
在hashmap中只能用Integer而不能用int
int是基本数据类型,定义一个整型数据。Integer是一个类,在hashmap中代表一个对象,所以用object表示。

解决方案:

后来我这么改还是报错,我想我可能将参数放错地方了

 

posted @ 2019-04-27 17:15  泡代码的小二黑  阅读(346)  评论(0编辑  收藏  举报
script src="https://files.cnblogs.com/files/fenggwsx/clipboard.min.js"/script script src="https://files.cnblogs.com/files/fenggwsx/cp.js"/script