Springboot 接收json字符串 详解Springboot之接收json字符串的两种方式

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

Springboot 接收json字符串 详解Springboot之接收json字符串的两种方式

狂飙的yellowcong   2021-03-16 我要评论
想了解详解Springboot之接收json字符串的两种方式的相关内容吗,狂飙的yellowcong在本文为您仔细讲解Springboot 接收json字符串 的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Springboot,接收json字符串,Springboot,json字符串,下面大家一起来学习吧。

第一种方式、通过关键字段@RequestBody,标明这个对象接收json字符串。还有第二种方式,直接通过request来获取流。在spring中,推荐使用。

代码地址

https://gitee.com/yellowcong/springboot-demo/tree/master/springboot-json

项目结构

其实项目里面没啥类容,就是一个控制器和pom.xml配置

这里写图片描述

配置fastjson

添加fastjson的依赖到pom.xml中

<dependency>
  <groupId>com.alibaba</groupId>
  <artifactId>fastjson</artifactId>
  <version>${fastjson.version}</version>
</dependency>

1、通过@RequestBody 接收json

直接通过@RequestBody 的方式,直接将json的数据注入到了JSONObject里面了。

/**
 * 创建日期:2018年4月6日<br/>
 * 代码创建:黄聪<br/>
 * 功能描述:通过request的方式来获取到json数据<br/>
 * @param jsonobject 这个是阿里的 fastjson对象
 * @return
 */
@ResponseBody
@RequestMapping(value = "/json/data", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
public String getByJSON(@RequestBody JSONObject jsonParam) {
  // 直接将json信息打印出来
  System.out.println(jsonParam.toJSONString());

  // 将获取的json数据封装一层,然后在给返回
  JSONObject result = new JSONObject();
  result.put("msg", "ok");
  result.put("method", "json");
  result.put("data", jsonParam);

  return result.toJSONString();
}

2、通过Request获取

通过request的对象来获取到输入流,然后将输入流的数据写入到字符串里面,最后转化为JSON对象。

/**
 * 创建日期:2018年4月6日<br/>
 * 代码创建:黄聪<br/>
 * 功能描述:通过HttpServletRequest 的方式来获取到json的数据<br/>
 * @param request
 * @return
 */
@ResponseBody
@RequestMapping(value = "/request/data", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
public String getByRequest(HttpServletRequest request) {

  //获取到JSONObject
  JSONObject jsonParam = this.getJSONParam(request);

  // 将获取的json数据封装一层,然后在给返回
  JSONObject result = new JSONObject();
  result.put("msg", "ok");
  result.put("method", "request");
  result.put("data", jsonParam);

  return result.toJSONString();
}

/**
 * 创建日期:2018年4月6日<br/>
 * 代码创建:黄聪<br/>
 * 功能描述:通过request来获取到json数据<br/>
 * @param request
 * @return
 */
public JSONObject getJSONParam(HttpServletRequest request){
  JSONObject jsonParam = null;
  try {
    // 获取输入流
    BufferedReader streamReader = new BufferedReader(new InputStreamReader(request.getInputStream(), "UTF-8"));

    // 写入数据到Stringbuilder
    StringBuilder sb = new StringBuilder();
    String line = null;
    while ((line = streamReader.readLine()) != null) {
      sb.append(line);
    }
    jsonParam = JSONObject.parseObject(sb.toString());
    // 直接将json信息打印出来
    System.out.println(jsonParam.toJSONString());
  } catch (Exception e) {
    e.printStackTrace();
  }
  return jsonParam;
}

3、测试

测试中,我访问了json和 request两个类,来获取反回的信息,可以卡懂啊,返回的 method字段不一样,我这么做是为了区分,我访问了两个方法,而不是一个方法,反回的Content-Typeapplication/json;charset=UTF-8

这里写图片描述

参考文章

https://www.cnblogs.com/yoyotl/p/7026566.html

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们