Spring Controller接收前端JSON數(shù)據(jù)請(qǐng)求方式
POST請(qǐng)求方式
1. 使用實(shí)體類接收
const http = require('http'); const postData = JSON.stringify({ "id": 1, "name": "三體", "price": 180 }); const options = { hostname: 'localhost', port: 8080, path: '/ReceiveJsonController/receiveJson1', method: 'POST', headers: { 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(postData) } } const req = http.request(options, res => { console.log(`狀態(tài)碼: ${res.statusCode}`) res.on('data', d => { process.stdout.write(d) }) }) req.on('error', error => { console.error(error) }) req.write(postData) req.end()
@PostMapping("/receiveJson1") public void receiveJson1(@RequestBody Book book, HttpServletRequest httpServletRequest) { logger.info("請(qǐng)求方式:" + httpServletRequest.getMethod()); logger.info("數(shù)據(jù):" + book); }
2. 使用List實(shí)體類接收
const http = require('http'); const postData = JSON.stringify([{ "id": 1, "name": "三體", "price": 180 },{ "id": 1, "name": "三體", "price": 180 },{ "id": 1, "name": "三體", "price": 180 },{ "id": 1, "name": "三體", "price": 180 },{ "id": 1, "name": "三體", "price": 180 },{ "id": 1, "name": "三體", "price": 180 },{ "id": 1, "name": "三體", "price": 180 },{ "id": 1, "name": "三體", "price": 180 },{ "id": 1, "name": "三體", "price": 180 },{ "id": 1, "name": "三體", "price": 180 },{ "id": 1, "name": "三體", "price": 180 },{ "id": 1, "name": "三體", "price": 180 }]); const options = { hostname: 'localhost', port: 8080, path: '/ReceiveJsonController/receiveJson2', method: 'POST', headers: { 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(postData) } } const req = http.request(options, res => { console.log(`狀態(tài)碼: ${res.statusCode}`) res.on('data', d => { process.stdout.write(d) }) }) req.on('error', error => { console.error(error) }) req.write(postData) req.end()
@PostMapping("/receiveJson2") public void receiveJson2(@RequestBody List<Book> books, HttpServletRequest httpServletRequest) { logger.info("請(qǐng)求方式:" + httpServletRequest.getMethod()); logger.info("數(shù)據(jù):" + books); }
3. 使用Map接收
const http = require('http'); const postData = JSON.stringify({ "data": { "id": 1, "name": "三體", "price": 180 } }); const options = { hostname: 'localhost', port: 8080, path: '/ReceiveJsonController/receiveJson3', method: 'POST', headers: { 'Content-Type': 'application/json', 'Content-Length': Buffer.byteLength(postData) } } const req = http.request(options, res => { console.log(`狀態(tài)碼: ${res.statusCode}`) res.on('data', d => { process.stdout.write(d) }) }) req.on('error', error => { console.error(error) }) req.write(postData) req.end()
@PostMapping("/receiveJson3") public void receiveJson3(@RequestBody Map<String, Object> paramsMap, HttpServletRequest httpServletRequest) { logger.info("請(qǐng)求方式:" + httpServletRequest.getMethod()); logger.info("數(shù)據(jù):" + paramsMap); }
使用Map接收,注意是Key:Value
形式
// 單個(gè)對(duì)象 { "data": { "id": 1, "name": "三體", "price": 180 } } // 多個(gè)對(duì)象 { "data": [{ "id": 1, "name": "三體", "price": 180 },{ "id": 1, "name": "三體", "price": 180 },{ "id": 1, "name": "三體", "price": 180 },{ "id": 1, "name": "三體", "price": 180 },{ "id": 1, "name": "三體", "price": 180 },{ "id": 1, "name": "三體", "price": 180 },{ "id": 1, "name": "三體", "price": 180 },{ "id": 1, "name": "三體", "price": 180 },{ "id": 1, "name": "三體", "price": 180 },{ "id": 1, "name": "三體", "price": 180 },{ "id": 1, "name": "三體", "price": 180 },{ "id": 1, "name": "三體", "price": 180 }] }
Get請(qǐng)求方式
get請(qǐng)求方式需要注意encodeURIComponent()
轉(zhuǎn)義,轉(zhuǎn)義后數(shù)據(jù)為一串字符串,所以只能使用String
接收,再使用Java json 工具類轉(zhuǎn)換成對(duì)應(yīng)的對(duì)象
const http = require('http') const options = { hostname: 'localhost', port: 8080, path: '/ReceiveJsonController/receiveJson5', method: 'GET' } let data = { "id": 1, "name": "三體", "price": 180 }; let jsonString = "?data=" + encodeURIComponent(JSON.stringify(data)); options.path = options.path + jsonString const req = http.request(options, res => { console.log(`狀態(tài)碼: ${res.statusCode}`) res.on('data', d => { process.stdout.write(d) }) }) req.on('error', error => { console.error(error) }) req.end()
@GetMapping("/receiveJson5") public void receiveJson5(@RequestParam("data") String data, HttpServletRequest httpServletRequest) { logger.info("請(qǐng)求方式:" + httpServletRequest.getMethod()); logger.info("數(shù)據(jù):" + data); }
總結(jié)
使用post請(qǐng)求傳遞json依然是最好的方式,用get也不是不可以,但是get有長(zhǎng)度限制。
以上就是Spring Controller接收前端JSON數(shù)據(jù)請(qǐng)求方式的詳細(xì)內(nèi)容,更多關(guān)于Spring Controller接收J(rèn)SON的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
mybatis多對(duì)多關(guān)聯(lián)實(shí)戰(zhàn)教程(推薦)
下面小編就為大家?guī)?lái)一篇mybatis多對(duì)多關(guān)聯(lián)實(shí)戰(zhàn)教程(推薦)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10java微信小程序步數(shù)encryptedData和開放數(shù)據(jù)解密的實(shí)現(xiàn)
這篇文章主要介紹了java微信小程序步數(shù)encryptedData和開放數(shù)據(jù)解密的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09使用Log4j2代碼方式配置實(shí)現(xiàn)線程級(jí)動(dòng)態(tài)控制
這篇文章主要介紹了使用Log4j2代碼方式配置實(shí)現(xiàn)線程級(jí)動(dòng)態(tài)控制,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12swagger的請(qǐng)求參數(shù)不顯示,@Apimodel的坑點(diǎn)及解決
這篇文章主要介紹了swagger的請(qǐng)求參數(shù)不顯示,@Apimodel的坑點(diǎn)及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-11-11Java超級(jí)實(shí)用的Freemarker工具類
這篇文章主要介紹了Java超級(jí)實(shí)用的Freemarker工具類,文章圍繞相關(guān)資料介紹以及代碼描述非常詳細(xì),需要的小伙伴可以參考一下,希望對(duì)你得學(xué)習(xí)有所幫助2022-02-02SpringCloud筆記(Hoxton)Netflix之Ribbon負(fù)載均衡示例代碼
這篇文章主要介紹了SpringCloud筆記HoxtonNetflix之Ribbon負(fù)載均衡,Ribbon是管理HTTP和TCP服務(wù)客戶端的負(fù)載均衡器,Ribbon具有一系列帶有名稱的客戶端(Named?Client),對(duì)SpringCloud?Ribbon負(fù)載均衡相關(guān)知識(shí)感興趣的朋友一起看看吧2022-06-06SpringBoot和Swagger結(jié)合提高API開發(fā)效率
這篇文章主要介紹了SpringBoot和Swagger結(jié)合提高API開發(fā)效率的相關(guān)資料,需要的朋友可以參考下2017-09-09Spring Cloud Feign實(shí)現(xiàn)動(dòng)態(tài)URL
本文主要介紹了Spring Cloud Feign實(shí)現(xiàn)動(dòng)態(tài)URL,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02IntelliJ IDEA快速創(chuàng)建getter和setter方法
這篇文章主要介紹了IntelliJ IDEA快速創(chuàng)建getter和setter方法,本文通過(guò)圖文實(shí)例相結(jié)合給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-03-03