最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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()轉義,轉義后數(shù)據(jù)為一串字符串,所以只能使用String接收,再使用Java json 工具類轉換成對應的對象

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);
}

總結

使用post請求傳遞json依然是最好的方式,用get也不是不可以,但是get有長度限制。

以上就是Spring Controller接收前端JSON數(shù)據(jù)請求方式的詳細內容,更多關于Spring Controller接收JSON的資料請關注腳本之家其它相關文章!

相關文章

  • springboot中一些比較常用的注解總結

    springboot中一些比較常用的注解總結

    今天給大家?guī)淼氖顷P于Java的相關知識,文章圍繞著springboot中一些比較常用的注解展開,文中有非常詳細的總結,需要的朋友可以參考下
    2021-06-06
  • 關于Java數(shù)組聲明、創(chuàng)建、初始化的相關介紹

    關于Java數(shù)組聲明、創(chuàng)建、初始化的相關介紹

    這篇文章主要是關于Java數(shù)組聲明、創(chuàng)建、初始化的相關介紹,并給出其對應的代碼,需要的朋友可以參考下
    2015-08-08
  • Java中的HashMap為什么會產生死循環(huán)

    Java中的HashMap為什么會產生死循環(huán)

    這篇文章主要介紹了Java中的HashMap為什么會產生死循環(huán),HashMap?死循環(huán)是一個比較常見、比較經(jīng)典的問題,下面文章我們就來徹底理解死循環(huán)的原因。需要的小伙伴可以參考一下
    2022-05-05
  • SpringBoot@Aspect 打印訪問請求和返回數(shù)據(jù)方式

    SpringBoot@Aspect 打印訪問請求和返回數(shù)據(jù)方式

    這篇文章主要介紹了SpringBoot@Aspect 打印訪問請求和返回數(shù)據(jù)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • java中的i++和++i的區(qū)別詳解

    java中的i++和++i的區(qū)別詳解

    這篇文章主要介紹了java中的i++和++i的區(qū)別詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-08-08
  • 從基礎到實戰(zhàn)詳解SpringBoot獲取資源文件全指南

    從基礎到實戰(zhàn)詳解SpringBoot獲取資源文件全指南

    在 SpringBoot 開發(fā)中,我們經(jīng)常需要訪問各類資源文件,本文將系統(tǒng)講解 SpringBoot 中資源文件的存放規(guī)則,獲取方法及實戰(zhàn)技巧,需要的可以了解下
    2025-07-07
  • Java實現(xiàn)Excel批量導入數(shù)據(jù)

    Java實現(xiàn)Excel批量導入數(shù)據(jù)

    這篇文章主要為大家詳細介紹了Java實現(xiàn)Excel批量導入數(shù)據(jù),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-08-08
  • 如何獲取java新IO的Path文件大小

    如何獲取java新IO的Path文件大小

    這篇文章主要介紹了如何獲取java新IO的Path文件大小,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-09-09
  • 如何在不使用spring框架中使用aop的功能

    如何在不使用spring框架中使用aop的功能

    這篇文章主要介紹了如何在不使用spring框架中使用aop的功能,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • Java多線程及分布式爬蟲架構原理解析

    Java多線程及分布式爬蟲架構原理解析

    這篇文章主要介紹了Java多線程及分布式爬蟲架構原理解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-10-10

最新評論

集安市| 波密县| 莫力| 会同县| 东山县| 宝兴县| 嘉义市| 光泽县| 朝阳市| 武功县| 南澳县| 正蓝旗| 兖州市| 孟州市| 安丘市| 大冶市| 巴林右旗| 白银市| 方城县| 宁德市| 德阳市| 汉阴县| 尉氏县| 安宁市| 福鼎市| 中江县| 凌源市| 定边县| 永平县| 游戏| 永安市| 宁武县| 鄂伦春自治旗| 峨山| 镇江市| 蚌埠市| 东城区| 广西| 交口县| 青神县| 东源县|