1. demo介绍

前几天测试接口的时候,以json的形式提交post请求数据,后台用一个实体类来接收参数,发现获取到的对象属性都为null。下面是我写的一个用于测试的小demo,来简单描述一下我所遇到的问题。

项目包含的目录结构如下:

User里面仅包含两个字段:username和password,如下:

UserController里面有一个测试接口,如下:

UserControllerTest是对该接口的测试,代码如下:

sendPost是对封装的一个POST请求函数,如下:

 

2. 问题描述

首先按照上面介绍的代码运行,后台是获取不到参数的,如下所示:

然后,使用postman进行请求也是同样的结果。问题的症结就是,为什么使用json传递请求参数时,后台实体接收不到参数,而使用表单提交时,却是可以接收到参数的。

这里说一下,下面这条语句是错误的:

因为,list.toString()不是json格式的,因此,使用fastjson将其转换成json格式的,再填充,修改如下:

 

3. 解决方案

(1)看到网上有人说要使用@RequestBody来接收,因此,修改代码如下:

运行结果:

  •  使用postman发送json请求,是可以得到请求参数。Json字符串如下:

  •  使用UserControllerTest的测试用例进行测试,得不到请求参数。后台输出一条警告,如下:

通过上面的警告信息我们可以看到,json解析失败。那么,初步怀疑可能还是json串的问题,将上述修改部分的结果单独打印出来,值如下所示:

备注:这里打印的是JSONObject.toJSON(list)的结果。显然,这并不是我们要传的json字符串。也就是说,使用json格式传参的时候,实际传入的并不是和实体类对应的json字符串,因此出现json解析失败。因此,修改代码如下:

运行程序,可以获取参数。

 

(2)通过上述方案,排除了测试代码的影响。但是,在之前一个公司写代码时,并没有使用@RequestBody,也是可以获取到json参数的。但是,我这里把@RequestBody去掉之后,是得不到json参数的,那这个又是什么原因呢?

首先通过百度,在这片博客中,遇到了类似的情况,但是使用了@RequestBody,和我们的问题还不一样,但是也值得看一看。链接如下:

https://blog.csdn.net/jiangjun0130/article/details/89210172

没办法,后来就去请教了前同事,才发现,之前的提交是采用表单提交的,所以没有用@RequestBody接收参数。

GitHub 加速计划 / js / json
18
5
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:3 个月前 )
f06604fc * :page_facing_up: bump the copyright years Signed-off-by: Niels Lohmann <mail@nlohmann.me> * :page_facing_up: bump the copyright years Signed-off-by: Niels Lohmann <mail@nlohmann.me> * :page_facing_up: bump the copyright years Signed-off-by: Niels Lohmann <niels.lohmann@gmail.com> --------- Signed-off-by: Niels Lohmann <mail@nlohmann.me> Signed-off-by: Niels Lohmann <niels.lohmann@gmail.com> 15 小时前
d23291ba * add a ci step for Json_Diagnostic_Positions Signed-off-by: Harinath Nampally <harinath922@gmail.com> * Update ci.cmake to address review comments Signed-off-by: Harinath Nampally <harinath922@gmail.com> * address review comment Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix typo in the comment Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix typos in ci.cmake Signed-off-by: Harinath Nampally <harinath922@gmail.com> * invoke the new ci step from ubuntu.yml Signed-off-by: Harinath Nampally <harinath922@gmail.com> * issue4561 - use diagnostic positions for exceptions Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix ci_test_documentation check Signed-off-by: Harinath Nampally <harinath922@gmail.com> * address review comments Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix ci check failures for unit-diagnostic-postions.cpp Signed-off-by: Harinath Nampally <harinath922@gmail.com> * improvements based on review comments Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix const correctness string Signed-off-by: Harinath Nampally <harinath922@gmail.com> * further refinements based on reviews Signed-off-by: Harinath Nampally <harinath922@gmail.com> * add one more test case for full coverage Signed-off-by: Harinath Nampally <harinath922@gmail.com> * ci check fix - add const Signed-off-by: Harinath Nampally <harinath922@gmail.com> * add unit tests for json_diagnostic_postions only Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix ci_test_diagnostics Signed-off-by: Harinath Nampally <harinath922@gmail.com> * fix ci_test_build_documentation check Signed-off-by: Harinath Nampally <harinath922@gmail.com> --------- Signed-off-by: Harinath Nampally <harinath922@gmail.com> 22 小时前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐