亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Spring Controller接收前端JSON數(shù)據(jù)請求方式

 更新時間:2023年07月20日 09:37:24   作者:李晗  
這篇文章主要為大家介紹了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ù)請求方式的詳細(xì)內(nèi)容,更多關(guān)于Spring Controller接收J(rèn)SON的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • mybatis多對多關(guān)聯(lián)實戰(zhà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)

    這篇文章主要介紹了java微信小程序步數(shù)encryptedData和開放數(shù)據(jù)解密的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-09-09
  • Java遞歸方法實現(xiàn)山脈繪制

    Java遞歸方法實現(xiàn)山脈繪制

    這篇文章主要為大家詳細(xì)介紹了Java遞歸方法實現(xiàn)山脈繪制,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • 使用Log4j2代碼方式配置實現(xiàn)線程級動態(tài)控制

    使用Log4j2代碼方式配置實現(xiàn)線程級動態(tài)控制

    這篇文章主要介紹了使用Log4j2代碼方式配置實現(xiàn)線程級動態(tài)控制,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • swagger的請求參數(shù)不顯示,@Apimodel的坑點及解決

    swagger的請求參數(shù)不顯示,@Apimodel的坑點及解決

    這篇文章主要介紹了swagger的請求參數(shù)不顯示,@Apimodel的坑點及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-11-11
  • Java超級實用的Freemarker工具類

    Java超級實用的Freemarker工具類

    這篇文章主要介紹了Java超級實用的Freemarker工具類,文章圍繞相關(guān)資料介紹以及代碼描述非常詳細(xì),需要的小伙伴可以參考一下,希望對你得學(xué)習(xí)有所幫助
    2022-02-02
  • SpringCloud筆記(Hoxton)Netflix之Ribbon負(fù)載均衡示例代碼

    SpringCloud筆記(Hoxton)Netflix之Ribbon負(fù)載均衡示例代碼

    這篇文章主要介紹了SpringCloud筆記HoxtonNetflix之Ribbon負(fù)載均衡,Ribbon是管理HTTP和TCP服務(wù)客戶端的負(fù)載均衡器,Ribbon具有一系列帶有名稱的客戶端(Named?Client),對SpringCloud?Ribbon負(fù)載均衡相關(guān)知識感興趣的朋友一起看看吧
    2022-06-06
  • SpringBoot和Swagger結(jié)合提高API開發(fā)效率

    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

    本文主要介紹了Spring Cloud Feign實現(xiàn)動態(tài)URL,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • IntelliJ IDEA快速創(chuàng)建getter和setter方法

    IntelliJ IDEA快速創(chuàng)建getter和setter方法

    這篇文章主要介紹了IntelliJ IDEA快速創(chuàng)建getter和setter方法,本文通過圖文實例相結(jié)合給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-03-03

最新評論