springboot接收json數(shù)據(jù)時,接收到空值問題
springboot接收json數(shù)據(jù)時,接收到空值
問題:springboot接收json數(shù)據(jù)收到空值
原因:springboot處理json數(shù)據(jù)時默認(rèn)采用小駝峰映射
案例
實體類:
@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收到空值:
這就是因為springboot處理json數(shù)據(jù)時默認(rèn)采用小駝峰映射
解決方法
方法一:
將實體類屬性名改成小駝峰命名;
方法二:
在實體類上面添加注解
@JsonNaming(PropertyNamingStrategy.UpperCamelCaseStrategy.class) //設(shè)置springboot序列化對象時使用大駝峰命名,springboot默認(rèn)使用小駝峰命名
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具體的對象的方式去接收,這種方式對于較復(fù)雜的json數(shù)據(jù)處理起來挺麻煩的
@PostMapping(value="/demoxxx") public void jsonStr6(@RequestBody Object object) { System.out.println(object.toString()); }
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java Web項目中編寫定時任務(wù)的實現(xiàn)
本篇文章主要介紹了Java Web項目中編寫定時任務(wù)的實現(xiàn),具有一定的參考價值,有興趣的可以了解一下。2017-01-01SpringBoot調(diào)用WebService接口的實現(xiàn)示例
本文主要介紹了SpringBoot調(diào)用WebService接口的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-03-03JAVA微信掃碼支付模式二線上支付功能實現(xiàn)以及回調(diào)
本篇文章主要介紹了JAVA微信掃碼支付模式二線上支付功能實現(xiàn)以及回調(diào),這里整理了詳細(xì)的代碼,有需要的小伙伴可以參考下。2016-11-11Spring Boot使用GridFS實現(xiàn)文件的上傳和下載方式
這篇文章主要介紹了Spring Boot使用GridFS實現(xiàn)文件的上傳和下載方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10