關(guān)于SpringBoot接收json格式的Demo案例
SpringBoot接收json格式的Demo
面向API接口開發(fā)的時候,經(jīng)常遇到對接接口數(shù)據(jù),而數(shù)據(jù)一般是json格式的
在這里記錄一下使用SpringBoot接收json格式數(shù)據(jù)的方式
使用SpringBoot的@RequestBody注解
將json數(shù)據(jù)用字符串去接收
然后轉(zhuǎn)成fastjson的對象(com.alibaba.fastjson.JSONObject)
package boot.example.json.controller; import com.alibaba.fastjson.JSONObject; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.HashMap; import java.util.Map; /** * 螞蟻舞 */ @RestController @RequestMapping(value="/boot") public class BootJsonStrController1 { @PostMapping(value="/demo1") public Object jsonStr1(@RequestBody String str) { // 使用fastjson JSONObject JSONObject jsonData = JSONObject.parseObject(str); System.out.println(jsonData.toJSONString()); Map<String, Object> map = new HashMap<>(); map.put("state", true); map.put("code", 200); map.put("timeStamp", System.currentTimeMillis()/1000); return map; } }
也可以用com.alibaba.fastjson2.JSONObject
package boot.example.json.controller; import com.alibaba.fastjson2.JSONObject; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.HashMap; import java.util.Map; /** * 螞蟻舞 */ @RestController @RequestMapping(value="/boot") public class BootJsonStrController2 { @PostMapping(value="/demo2") public Object jsonStr2(@RequestBody String str) { // 使用fastjson2 JSONObject JSONObject jsonData = JSONObject.parseObject(str); System.out.println(jsonData.toJSONString()); Map<String, Object> map = new HashMap<>(); map.put("state", true); map.put("code", 200); map.put("timeStamp", System.currentTimeMillis()/1000); return map; } }
fastjson的maven包
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>2.0.25</version> <scope>compile</scope> </dependency>
還可以使用(com.google.gson.JsonObject)
maven包
<dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.5</version> </dependency>
package boot.example.json.controller; import com.google.gson.Gson; import com.google.gson.JsonObject; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.HashMap; import java.util.Map; /** * 螞蟻舞 */ @RestController @RequestMapping(value="/boot") public class BootJsonStrController3 { @PostMapping(value="/demo3") public Object jsonStr3(@RequestBody String str) { Gson gson = new Gson(); JsonObject json = gson.fromJson(str, JsonObject.class); System.out.println(json.toString()); Map<String, Object> map = new HashMap<>(); map.put("state", true); map.put("code", 200); map.put("timeStamp", System.currentTimeMillis()/1000); return map; } }
直接使用fastjson的JSONObject對象
package boot.example.json.controller; import com.alibaba.fastjson.JSONObject; //import com.alibaba.fastjson2.JSONObject; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.util.HashMap; import java.util.Map; /** * 螞蟻舞 */ @RestController @RequestMapping(value="/boot") public class BootJsonStrController4 { @PostMapping(value="/demo4") public Object jsonStr4(@RequestBody JSONObject jsonObject) { System.out.println(jsonObject.toString()); Map<String, Object> map = new HashMap<>(); map.put("state", true); map.put("code", 200); map.put("timeStamp", System.currentTimeMillis()/1000); return map; } }
能不能使用com.google.gson.JsonObject對象去接收?
不能直接用?。?!
(有其他方式可用,就不去研究這種情況了)
import com.google.gson.JsonObject // 直接用是不行的 @PostMapping(value="/demoxxx") public void jsonStr5(@RequestBody JsonObject json) { System.out.println(json.toString()); }
簡單的json數(shù)據(jù)還可以用java具體的對象的方式去接收
這種方式對于較復雜的json數(shù)據(jù)處理起來挺麻煩的
@PostMapping(value="/demoxxx") public void jsonStr6(@RequestBody Object object) { System.out.println(object.toString()); }
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Cloud應(yīng)用實現(xiàn)配置自動刷新過程詳解
這篇文章主要介紹了Spring Cloud應(yīng)用實現(xiàn)配置自動刷新過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-12-12Spring中實現(xiàn)策略模式的幾種方式小結(jié)
在寫業(yè)務(wù)代碼的時候,難免會遇到很多if-else,這個時候如果if-else不是很多可以用if-else,如果此時場景過多,太多的if-else會導致代碼比較臃腫,這個時候策略模式就出現(xiàn)了,本文主要闡述工作中常用的實現(xiàn)策略模式的幾種方式,需要的朋友可以參考下2024-05-05Java ThreadLocal原理解析以及應(yīng)用場景分析案例詳解
這篇文章主要介紹了Java ThreadLocal原理解析以及應(yīng)用場景分析案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-09-09淺析Java中XPath和JsonPath以及SpEL的用法與對比
XPath,即XML路徑語言,是一種用于在XML文檔中查找信息的語言,JsonPath是從XPath中發(fā)展而來的,專門用于JSON數(shù)據(jù)格式,本文主要來講講他們的用法與區(qū)別,需要的可以參考下2023-11-11springBoot配置國產(chǎn)達夢數(shù)據(jù)庫的示例詳解
本文向大家介紹springBoot?配置國產(chǎn)達夢數(shù)據(jù)庫的相關(guān)知識,文章結(jié)合示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-04-04