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

 更新时间:2023年07月20日 09:37:24   作者:李晗  
这篇文章主要为大家介绍了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的资料请关注脚本之家其它相关文章!

相关文章

  • mybatis多对多关联实战教程(推荐)

    mybatis多对多关联实战教程(推荐)

    下面小编就为大家带来一篇mybatis多对多关联实战教程(推荐)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-10-10
  • java微信小程序步数encryptedData和开放数据解密的实现

    java微信小程序步数encryptedData和开放数据解密的实现

    这篇文章主要介绍了java微信小程序步数encryptedData和开放数据解密的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-09-09
  • Java递归方法实现山脉绘制

    Java递归方法实现山脉绘制

    这篇文章主要为大家详细介绍了Java递归方法实现山脉绘制,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-01-01
  • 使用Log4j2代码方式配置实现线程级动态控制

    使用Log4j2代码方式配置实现线程级动态控制

    这篇文章主要介绍了使用Log4j2代码方式配置实现线程级动态控制,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-12-12
  • swagger的请求参数不显示,@Apimodel的坑点及解决

    swagger的请求参数不显示,@Apimodel的坑点及解决

    这篇文章主要介绍了swagger的请求参数不显示,@Apimodel的坑点及解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-11-11
  • Java超级实用的Freemarker工具类

    Java超级实用的Freemarker工具类

    这篇文章主要介绍了Java超级实用的Freemarker工具类,文章围绕相关资料介绍以及代码描述非常详细,需要的小伙伴可以参考一下,希望对你得学习有所帮助
    2022-02-02
  • SpringCloud笔记(Hoxton)Netflix之Ribbon负载均衡示例代码

    SpringCloud笔记(Hoxton)Netflix之Ribbon负载均衡示例代码

    这篇文章主要介绍了SpringCloud笔记HoxtonNetflix之Ribbon负载均衡,Ribbon是管理HTTP和TCP服务客户端的负载均衡器,Ribbon具有一系列带有名称的客户端(Named Client),对SpringCloud Ribbon负载均衡相关知识感兴趣的朋友一起看看吧
    2022-06-06
  • SpringBoot和Swagger结合提高API开发效率

    SpringBoot和Swagger结合提高API开发效率

    这篇文章主要介绍了SpringBoot和Swagger结合提高API开发效率的相关资料,需要的朋友可以参考下
    2017-09-09
  • Spring Cloud Feign实现动态URL

    Spring Cloud Feign实现动态URL

    本文主要介绍了Spring Cloud Feign实现动态URL,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-02-02
  • IntelliJ IDEA快速创建getter和setter方法

    IntelliJ IDEA快速创建getter和setter方法

    这篇文章主要介绍了IntelliJ IDEA快速创建getter和setter方法,本文通过图文实例相结合给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-03-03

最新评论