Springboot之接收json字符串的两种方式-yellowcong

本文介绍了两种在SpringBoot中接收JSON数据的方法:一种是通过@RequestBody注解直接将JSON字符串映射为对象;另一种是通过HttpServletRequest获取输入流并转换为JSON对象。文中还提供了测试示例及代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第一种方式、通过关键字段@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

在$.each()中,要退出循环有两种方式。第一种是使用return true,这会跳至下一个循环,相当于在普通的循环中使用continue。第二种是使用return false,这会停止循环,相当于在普通的循环中使用break。\[1\]\[3\]具体来说,如果你想跳过当前循环,可以在循环体中使用return true。如果你想停止循环,可以在循环体中使用return false。\[2\]请注意,直接在$.each()中使用return或return value是不会直接退出该方法的,还是会继续执行循环。只有在使用return true或return false时,才会有特殊意义并且能够实现跳过或停止循环的效果。 #### 引用[.reference_title] - *1* [$.each使用以及return存在的坑](https://blog.youkuaiyun.com/qq_40555277/article/details/108229875)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [Js之$.each退出循环-yellowcong](https://blog.youkuaiyun.com/yelllowcong/article/details/78269935)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

狂飙的yellowcong

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值