Spring Controller接收前端JSON數(shù)據(jù)請求方式
POST請求方式
1. 使用實體類接收
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("請求方式:" + httpServletRequest.getMethod());
logger.info("數(shù)據(jù):" + book);
}2. 使用List實體類接收
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("請求方式:" + 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("請求方式:" + httpServletRequest.getMethod());
logger.info("數(shù)據(jù):" + paramsMap);
}使用Map接收,注意是Key:Value形式
// 單個對象
{
"data": {
"id": 1,
"name": "三體",
"price": 180
}
}
// 多個對象
{
"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請求方式
get請求方式需要注意encodeURIComponent()轉(zhuǎn)義,轉(zhuǎn)義后數(shù)據(jù)為一串字符串,所以只能使用String接收,再使用Java json 工具類轉(zhuǎn)換成對應(yīng)的對象
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("請求方式:" + httpServletRequest.getMethod());
logger.info("數(shù)據(jù):" + data);
}總結(jié)
使用post請求傳遞json依然是最好的方式,用get也不是不可以,但是get有長度限制。
以上就是Spring Controller接收前端JSON數(shù)據(jù)請求方式的詳細內(nèi)容,更多關(guān)于Spring Controller接收JSON的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
mybatis多對多關(guān)聯(lián)實戰(zhàn)教程(推薦)
下面小編就為大家?guī)硪黄猰ybatis多對多關(guān)聯(lián)實戰(zhàn)教程(推薦)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-10-10
java微信小程序步數(shù)encryptedData和開放數(shù)據(jù)解密的實現(xiàn)
這篇文章主要介紹了java微信小程序步數(shù)encryptedData和開放數(shù)據(jù)解密的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-09-09
使用Log4j2代碼方式配置實現(xiàn)線程級動態(tài)控制
這篇文章主要介紹了使用Log4j2代碼方式配置實現(xiàn)線程級動態(tài)控制,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12
swagger的請求參數(shù)不顯示,@Apimodel的坑點及解決
這篇文章主要介紹了swagger的請求參數(shù)不顯示,@Apimodel的坑點及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11
SpringCloud筆記(Hoxton)Netflix之Ribbon負載均衡示例代碼
這篇文章主要介紹了SpringCloud筆記HoxtonNetflix之Ribbon負載均衡,Ribbon是管理HTTP和TCP服務(wù)客戶端的負載均衡器,Ribbon具有一系列帶有名稱的客戶端(Named?Client),對SpringCloud?Ribbon負載均衡相關(guān)知識感興趣的朋友一起看看吧2022-06-06
SpringBoot和Swagger結(jié)合提高API開發(fā)效率
這篇文章主要介紹了SpringBoot和Swagger結(jié)合提高API開發(fā)效率的相關(guān)資料,需要的朋友可以參考下2017-09-09
Spring Cloud Feign實現(xiàn)動態(tài)URL
本文主要介紹了Spring Cloud Feign實現(xiàn)動態(tài)URL,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02
IntelliJ IDEA快速創(chuàng)建getter和setter方法
這篇文章主要介紹了IntelliJ IDEA快速創(chuàng)建getter和setter方法,本文通過圖文實例相結(jié)合給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03

