詳解Springboot之接收json字符串的兩種方式
第一種方式、通過關(guān)鍵字段@RequestBody,標(biāo)明這個(gè)對象接收json字符串。還有第二種方式,直接通過request來獲取流。在spring中,推薦使用。
代碼地址
https://gitee.com/yellowcong/springboot-demo/tree/master/springboot-json
項(xiàng)目結(jié)構(gòu)
其實(shí)項(xiàng)目里面沒啥類容,就是一個(gè)控制器和pom.xml配置

配置fastjson
添加fastjson的依賴到pom.xml中
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>${fastjson.version}</version></dependency>
1、通過@RequestBody 接收json
直接通過@RequestBody 的方式,直接將json的數(shù)據(jù)注入到了JSONObject里面了。
/** * 創(chuàng)建日期:2018年4月6日<br/> * 代碼創(chuàng)建:黃聰<br/> * 功能描述:通過request的方式來獲取到j(luò)son數(shù)據(jù)<br/> * @param jsonobject 這個(gè)是阿里的 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數(shù)據(jù)封裝一層,然后在給返回 JSONObject result = new JSONObject(); result.put('msg', 'ok'); result.put('method', 'json'); result.put('data', jsonParam); return result.toJSONString();}
2、通過Request獲取
通過request的對象來獲取到輸入流,然后將輸入流的數(shù)據(jù)寫入到字符串里面,最后轉(zhuǎn)化為JSON對象。
/** * 創(chuàng)建日期:2018年4月6日<br/> * 代碼創(chuàng)建:黃聰<br/> * 功能描述:通過HttpServletRequest 的方式來獲取到j(luò)son的數(shù)據(jù)<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數(shù)據(jù)封裝一層,然后在給返回 JSONObject result = new JSONObject(); result.put('msg', 'ok'); result.put('method', 'request'); result.put('data', jsonParam); return result.toJSONString();}/** * 創(chuàng)建日期:2018年4月6日<br/> * 代碼創(chuàng)建:黃聰<br/> * 功能描述:通過request來獲取到j(luò)son數(shù)據(jù)<br/> * @param request * @return */public JSONObject getJSONParam(HttpServletRequest request){ JSONObject jsonParam = null; try { // 獲取輸入流 BufferedReader streamReader = new BufferedReader(new InputStreamReader(request.getInputStream(), 'UTF-8')); // 寫入數(shù)據(jù)到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兩個(gè)類,來獲取反回的信息,可以卡懂啊,返回的 method字段不一樣,我這么做是為了區(qū)分,我訪問了兩個(gè)方法,而不是一個(gè)方法,反回的Content-Type 是application/json;charset=UTF-8

參考文章
https://www.cnblogs.com/yoyotl/p/7026566.html
到此這篇關(guān)于Springboot之接收json字符串的兩種方式的文章就介紹到這了,更多相關(guān)Springboot之接收json字符串的兩種方式內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. js實(shí)現(xiàn)碰撞檢測2. 一文帶你徹底理解Java序列化和反序列化3. JS實(shí)現(xiàn)前端動態(tài)分頁碼代碼實(shí)例4. 關(guān)于IDEA 2020.3 多窗口視圖丟失的問題5. PHP驗(yàn)證碼工具-Securimage6. 用Spring JMS使異步消息變得簡單7. 通過實(shí)例解析Python文件操作實(shí)現(xiàn)步驟8. Python3 json模塊之編碼解碼方法講解9. Python 制作查詢商品歷史價(jià)格的小工具10. Python 解析庫json及jsonpath pickle的實(shí)現(xiàn)

網(wǎng)公網(wǎng)安備