網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
原因
Spring Boot的注解@RequestParam不支持接受請(qǐng)求體內(nèi)的json數(shù)據(jù)。
以下為轉(zhuǎn)載他人部分:https://blog.csdn.net/qq_39506912/article/details/90107756
重點(diǎn)為對(duì)三個(gè)注解的比較
錯(cuò)誤分析
使用Post向接口發(fā)送json數(shù)據(jù)時(shí)顯示如下錯(cuò)誤:
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ù)并無(wú)格式問(wèn)題,查閱資料發(fā)現(xiàn)是接口接受傳參問(wèn)題,我這里使用了@RequestParam注解來(lái)接受數(shù)據(jù),而這個(gè)注解不支持接受請(qǐng)求體內(nèi)的json數(shù)據(jù)。
@PostMapping("/account")
public Object insertAccount(@RequestParam("userName") String userName) {
...
解決方法
下面是對(duì)@PathVariable
,@RequestParam
,@RequestBody
三者的比較
注解 | 支持的類型 | 支持的請(qǐng)求類型 | 支持的Content-Type | 請(qǐng)求示例 |
@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請(qǐng)求數(shù)據(jù),而后將接收到的json數(shù)據(jù)轉(zhuǎn)化為json對(duì)象,可以使用json對(duì)象的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-05-17 docker停止某個(gè)容器
- 2022-12-14 正則表達(dá)式匹配0-10的正整數(shù)以及使用細(xì)節(jié)_正則表達(dá)式
- 2022-07-08 一文詳解C++中運(yùn)算符的使用_C 語(yǔ)言
- 2022-05-12 Python中賦值運(yùn)算符的含義與使用方法_python
- 2022-05-25 Postman動(dòng)態(tài)獲取值(動(dòng)態(tài)設(shè)置全局變量)
- 2022-06-16 利用Jetpack?Compose實(shí)現(xiàn)繪制五角星效果_Android
- 2022-07-19 安卓TextView的lineHeight*lineCount!=height問(wèn)題,解決不支持滾動(dòng)的
- 2022-07-11 Android星級(jí)評(píng)分條實(shí)現(xiàn)評(píng)分界面_Android
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- 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)證過(guò)濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤: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)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支