java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > Spring Controller接收JSON

Spring Controller接收前端JSON数据请求方式

作者:李晗

这篇文章主要为大家介绍了Spring Controller接收前端JSON数据请求方式详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

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(`状态码: ${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("数据:" + 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(`状态码: ${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("数据:" + 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(`状态码: ${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("数据:" + 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()转义,转义后数据为一串字符串,所以只能使用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(`状态码: ${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("数据:" + data);
}

总结

使用post请求传递json依然是最好的方式,用get也不是不可以,但是get有长度限制。

以上就是Spring Controller接收前端JSON数据请求方式的详细内容,更多关于Spring Controller接收JSON的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
阅读全文