關(guān)于SpringBoot接收json格式的Demo案例
SpringBoot接收json格式的Demo
面向API接口開發(fā)的時(shí)候,經(jīng)常遇到對(duì)接接口數(shù)據(jù),而數(shù)據(jù)一般是json格式的
在這里記錄一下使用SpringBoot接收json格式數(shù)據(jù)的方式
使用SpringBoot的@RequestBody注解
將json數(shù)據(jù)用字符串去接收
然后轉(zhuǎn)成fastjson的對(duì)象(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對(duì)象
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對(duì)象去接收?
不能直接用?。。?/strong>
(有其他方式可用,就不去研究這種情況了)
import com.google.gson.JsonObject
// 直接用是不行的
@PostMapping(value="/demoxxx")
public void jsonStr5(@RequestBody JsonObject json) {
System.out.println(json.toString());
}簡單的json數(shù)據(jù)還可以用java具體的對(duì)象的方式去接收
這種方式對(duì)于較復(fù)雜的json數(shù)據(jù)處理起來挺麻煩的
@PostMapping(value="/demoxxx")
public void jsonStr6(@RequestBody Object object) {
System.out.println(object.toString());
}總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Spring Cloud應(yīng)用實(shí)現(xiàn)配置自動(dòng)刷新過程詳解
這篇文章主要介紹了Spring Cloud應(yīng)用實(shí)現(xiàn)配置自動(dòng)刷新過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12
Spring中實(shí)現(xiàn)策略模式的幾種方式小結(jié)
在寫業(yè)務(wù)代碼的時(shí)候,難免會(huì)遇到很多if-else,這個(gè)時(shí)候如果if-else不是很多可以用if-else,如果此時(shí)場景過多,太多的if-else會(huì)導(dǎo)致代碼比較臃腫,這個(gè)時(shí)候策略模式就出現(xiàn)了,本文主要闡述工作中常用的實(shí)現(xiàn)策略模式的幾種方式,需要的朋友可以參考下2024-05-05
java學(xué)生信息管理系統(tǒng)設(shè)計(jì)(2)
這篇文章主要為大家詳細(xì)介紹了java學(xué)生信息管理系統(tǒng)設(shè)計(jì),學(xué)生信息添加進(jìn)入數(shù)據(jù)庫的事務(wù),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-11-11
Java ThreadLocal原理解析以及應(yīng)用場景分析案例詳解
這篇文章主要介紹了Java ThreadLocal原理解析以及應(yīng)用場景分析案例詳解,本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-09-09
淺析Java中XPath和JsonPath以及SpEL的用法與對(duì)比
XPath,即XML路徑語言,是一種用于在XML文檔中查找信息的語言,JsonPath是從XPath中發(fā)展而來的,專門用于JSON數(shù)據(jù)格式,本文主要來講講他們的用法與區(qū)別,需要的可以參考下2023-11-11
springBoot配置國產(chǎn)達(dá)夢(mèng)數(shù)據(jù)庫的示例詳解
本文向大家介紹springBoot?配置國產(chǎn)達(dá)夢(mèng)數(shù)據(jù)庫的相關(guān)知識(shí),文章結(jié)合示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-04-04

