springboot开发中 以POST方式向后台传递json数据的时候参数值为null

4 篇文章 0 订阅
3 篇文章 0 订阅

自己写了一个简单的springboot后台代码,当我用postman以post方式向后台传递json对象时,后台对象的参数值总是为null,找了好久终于解决了,下面分享一下我的解决问题艰辛路程。

明明我传了参数值,但是debug调试的时候就是获取不到。

1.首先,我的controller中在参数前加了@RequestBody

具体原因参考:https://blog.csdn.net/u012843873/article/details/80243340,以下是具体内容(害怕博主删了找不到,干脆就直接粘贴过来了)

/**************************帖子内容************************************************/

我的错误原因就是因为,在spring注解开发中,使用get或者是post请求的时候,就是需要这些注意事项 
这里写图片描述 
这里写图片描述

这里写图片描述

就是这两个地方搞混,后台就会拿不到前台传递的json数据

/**************************帖子内容************************************************/

2.修改完上面代码,一运行又报了这个错

“Content type 'text/plain;charset=UTF-8' not supported”,网上又是一通百度,找到的答案是数据传输格式选择错误,修改为下面即可。

è¿éåå¾çæè¿°

3.以上是通过postman测试的,具体放到前台项目中,因为我前台做的是跨域访问,又遇到了一系列的问题。

a.错误信息:Resolved exception caused by handler execution: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Unrecognized token 'name': was expecting 'null', 'true', 'false' or NaN; nested exception is com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'name': was expecting 'null', 'true', 'false' or NaN

原因前台ajax请求传递的data参数格式不正确。

解决方法参考:https://blog.csdn.net/javaee_sunny/article/details/52576710

b.错误信息:Access to XMLHttpRequest at 'http://localhost:8080/api/user/addUser' from origin 'http://localhost:8088' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.

这个就是跨域请求出错的问题。解决方法参考:https://blog.csdn.net/m0_37836194/article/details/79173463,https://blog.csdn.net/xuedapeng/article/details/79076704

 

  • 3
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值