springboot接收json數(shù)據(jù)時(shí),接收到空值問(wèn)題
springboot接收json數(shù)據(jù)時(shí),接收到空值
問(wèn)題:springboot接收json數(shù)據(jù)收到空值
原因:springboot處理json數(shù)據(jù)時(shí)默認(rèn)采用小駝峰映射
案例
實(shí)體類:
@Data public class NewTask { private String TaskNo; private String Priority; private String VehicleNo; private String VehicleType; private String FinishAction; private String TaskType; private String Location; }
Controller:
@PostMapping("/addTask") public String addTask(@RequestBody NewTask task){ System.out.println(task.toString()); return "ss"; }
postmam:
springboot收到空值:
這就是因?yàn)閟pringboot處理json數(shù)據(jù)時(shí)默認(rèn)采用小駝峰映射
解決方法
方法一:
將實(shí)體類屬性名改成小駝峰命名;
方法二:
在實(shí)體類上面添加注解
@JsonNaming(PropertyNamingStrategy.UpperCamelCaseStrategy.class) //設(shè)置springboot序列化對(duì)象時(shí)使用大駝峰命名,springboot默認(rèn)使用小駝峰命名
springboot接收json格式的Demo案例
面向API接口開(kāi)發(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ù)處理起來(lái)挺麻煩的
@PostMapping(value="/demoxxx") public void jsonStr6(@RequestBody Object object) { System.out.println(object.toString()); }
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java中冒泡排序的原生實(shí)現(xiàn)方法(正序與逆序)
這篇文章主要給大家介紹了關(guān)于Java中冒泡排序的原生實(shí)現(xiàn)方法(正序與逆序)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11Java Web項(xiàng)目中編寫(xiě)定時(shí)任務(wù)的實(shí)現(xiàn)
本篇文章主要介紹了Java Web項(xiàng)目中編寫(xiě)定時(shí)任務(wù)的實(shí)現(xiàn),具有一定的參考價(jià)值,有興趣的可以了解一下。2017-01-01SpringBoot調(diào)用WebService接口的實(shí)現(xiàn)示例
本文主要介紹了SpringBoot調(diào)用WebService接口的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-03-03JAVA微信掃碼支付模式二線上支付功能實(shí)現(xiàn)以及回調(diào)
本篇文章主要介紹了JAVA微信掃碼支付模式二線上支付功能實(shí)現(xiàn)以及回調(diào),這里整理了詳細(xì)的代碼,有需要的小伙伴可以參考下。2016-11-11哈希表在算法題目中的實(shí)際應(yīng)用詳解(Java)
散列表(Hash?table,也叫哈希表)是根據(jù)關(guān)鍵碼值(Key?value)而直接進(jìn)行訪問(wèn)的數(shù)據(jù)結(jié)構(gòu),下面這篇文章主要給大家介紹了關(guān)于哈希表在算法題目中的實(shí)際應(yīng)用,文中介紹的方法是Java,需要的朋友可以參考下2024-03-03Spring Boot使用GridFS實(shí)現(xiàn)文件的上傳和下載方式
這篇文章主要介紹了Spring Boot使用GridFS實(shí)現(xiàn)文件的上傳和下載方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10