關(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ì)象去接收?
不能直接用?。?!
(有其他方式可用,就不去研究這種情況了)
import com.google.gson.JsonObject
// 直接用是不行的
@PostMapping(value="/demoxxx")
public void jsonStr5(@RequestBody JsonObject json) {
System.out.println(json.toString());
}簡(jiǎn)單的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)文章
IDEA修改idea.vmoptions后,IDEA無法打開的解決方案
文章介紹了在IDEA中因錯(cuò)誤修改啟動(dòng)參數(shù)導(dǎo)致無法啟動(dòng)的問題,指出正確的修改文件位置應(yīng)在破解插件目錄下的idea.vmoptions,并分享了個(gè)人經(jīng)驗(yàn)供參考2025-10-10
eclipse/IDEA配置javafx項(xiàng)目步驟(圖文教程)
這篇文章主要介紹了eclipse/IDEA配置javafx項(xiàng)目步驟(圖文教程),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
Java基礎(chǔ)知識(shí)精通循環(huán)結(jié)構(gòu)與break及continue
循環(huán)結(jié)構(gòu)是指在程序中需要反復(fù)執(zhí)行某個(gè)功能而設(shè)置的一種程序結(jié)構(gòu)。它由循環(huán)體中的條件,判斷繼續(xù)執(zhí)行某個(gè)功能還是退出循環(huán),選擇結(jié)構(gòu)用于判斷給定的條件,根據(jù)判斷的結(jié)果判斷某些條件,根據(jù)判斷的結(jié)果來控制程序的流程2022-04-04
Spring Boot實(shí)現(xiàn)郵件注冊(cè)功能示例代碼
本篇文章主要介紹了Spring Boot實(shí)現(xiàn)郵件注冊(cè)功能示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-05-05
Java 實(shí)現(xiàn)分布式服務(wù)的調(diào)用鏈跟蹤
分布式服務(wù)中完成某一個(gè)業(yè)務(wù)動(dòng)作,需要服務(wù)之間的相互協(xié)作才能完成,在這一次動(dòng)作引起的多服務(wù)的聯(lián)動(dòng)我們需要用1個(gè)唯一標(biāo)識(shí)關(guān)聯(lián)起來,關(guān)聯(lián)起來就是調(diào)用鏈的跟蹤。本文介紹了Java 實(shí)現(xiàn)分布式服務(wù)的調(diào)用鏈跟蹤的步驟2021-06-06
解決FastJson中"$ref重復(fù)引用"的問題方法
這篇文章主要介紹了解決FastJson中"$ref重復(fù)引用"的問題方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-11-11

