springboot接收json數(shù)據(jù)時,接收到空值問題
springboot接收json數(shù)據(jù)時,接收到空值
問題:springboot接收json數(shù)據(jù)收到空值
原因:springboot處理json數(shù)據(jù)時默認采用小駝峰映射
案例
實體類:
@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ù)時默認采用小駝峰映射
解決方法
方法一:
將實體類屬性名改成小駝峰命名;
方法二:
在實體類上面添加注解
@JsonNaming(PropertyNamingStrategy.UpperCamelCaseStrategy.class) //設(shè)置springboot序列化對象時使用大駝峰命名,springboot默認使用小駝峰命名
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)文章
Springboot使用SPI注冊bean到spring容器的示例代碼
這篇文章主要介紹了Springboot使用SPI注冊bean到spring容器,主要包括mydriver接口,mysqldriver實現(xiàn)過程,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-10-10
Mybatis使用XML實現(xiàn)動態(tài)sql的示例代碼
當(dāng)編寫 MyBatis 中復(fù)雜動態(tài) SQL 語句時,使用 XML 格式是一種非常靈活的方式,本文主要為大家詳細介紹了Mybatis使用XML實現(xiàn)動態(tài)sql的具體方法,需要的可以參考下2023-12-12
Java實現(xiàn)List與數(shù)組互轉(zhuǎn)(Arrays.asList與Collectors.toList)的兩種方法
在 Java 編程中,List 和數(shù)組(Array)是兩種常用的數(shù)據(jù)結(jié)構(gòu),本文將深入探討 List 與數(shù)組之間的相互轉(zhuǎn)換,重點介紹 Arrays.asList 和 Collectors.toList 這兩種常用且重要的方法,并分析它們的特點、適用場景及注意事項,需要的朋友可以參考下2026-01-01
Springboot整合Netty自定義協(xié)議實現(xiàn)示例詳解
這篇文章主要為大家介紹了Springboot整合Netty自定義協(xié)議實現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11

