網(wǎng)站首頁 編程語言 正文
原因
Spring Boot的注解@RequestParam不支持接受請求體內(nèi)的json數(shù)據(jù)。
以下為轉(zhuǎn)載他人部分:https://blog.csdn.net/qq_39506912/article/details/90107756
重點(diǎn)為對三個注解的比較
錯誤分析
使用Post向接口發(fā)送json數(shù)據(jù)時顯示如下錯誤:
WARN 13392 --- [nio-8080-exec-1] .w.s.m.s.DefaultHandlerExceptionResolver : Resolved exception caused by handler execution: org.springframework.web.bind.MissingServletRequestParameterException:
Required String parameter 'userName' is not present
查看json數(shù)據(jù)并無格式問題,查閱資料發(fā)現(xiàn)是接口接受傳參問題,我這里使用了@RequestParam注解來接受數(shù)據(jù),而這個注解不支持接受請求體內(nèi)的json數(shù)據(jù)。
@PostMapping("/account")
public Object insertAccount(@RequestParam("userName") String userName) {
...
解決方法
下面是對@PathVariable
,@RequestParam
,@RequestBody
三者的比較
注解 | 支持的類型 | 支持的請求類型 | 支持的Content-Type | 請求示例 |
@PathVariable | url | GET | 所有 | /test/{id} |
@RequestParam | url | GET | 所有 | /test?id=1 |
Body | POST/PUT/DELETE/PATCH | form-data或x-www.form-urlencoded | id:1 | |
@RequestBody | Body | POST/PUT/DELETE/PATCH | json | {"id":1} |
將接口改成以@RequestBody
注解方式接受json請求數(shù)據(jù),而后將接收到的json數(shù)據(jù)轉(zhuǎn)化為json對象,可以使用json對象的get()
方法取得參數(shù)值,代碼如下:
@PostMapping("/account")
public Object insertAccount(@RequestBody JSONObject jsonParam) {
String userName=jsonParam.get("userName").toString()
...
原文鏈接:https://blog.csdn.net/qq_36256590/article/details/121822876
相關(guān)推薦
- 2022-09-14 jQuery實(shí)現(xiàn)簡易計(jì)算器功能_jquery
- 2023-07-02 Python3中zip()函數(shù)知識點(diǎn)小結(jié)_python
- 2022-06-08 VM配置Centos7虛擬機(jī)
- 2022-11-19 Python+random模塊實(shí)現(xiàn)隨機(jī)抽樣_python
- 2021-12-09 android藍(lán)牙簡單開發(fā)示例教程_Android
- 2022-07-12 微信小程序(條件渲染和列表渲染)
- 2022-12-23 Android同步異步任務(wù)與多線程及Handler消息處理機(jī)制基礎(chǔ)詳細(xì)講解_Android
- 2022-07-30 golang?redis中Pipeline通道的使用詳解_Golang
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支