聊聊@RequestBody和Json之間的關(guān)系
在使用springmvc的時候,后臺@RequestBody接受的是一個json格式的字符串,一定是一個字符串。
我們可以通過@RequestBody Map
@RequestMapping(value="/queryAccountList.do",produces="application/json;charset=UTF-8") @ResponseBody public HashMap<String, Object> queryAccountList(@RequestBody Map<String, Object> paramsMap){ System.out.println("paramsMap="+paramsMap); String channel= (String) paramsMap.get("channel"); String function_code=(String) paramsMap.get("function_code"); Map<String, Object> reqParam=(Map<String, Object>)paramsMap.get("data");
當(dāng)前端調(diào)用我們的接口時,傳入json字符串,就轉(zhuǎn)為了map對象。這里主要是@RequestBody的底層實現(xiàn),我們不討論。
json對象和json字符串的區(qū)別:
var person={“name”:”zhangsan”,”sex”:”男”,”age”:”24”}//json對象 var person='{“name”:”zhangsan”,”sex”:”男”,”age”:”24”}';//json字符串
json對象轉(zhuǎn)為json字符串,調(diào)用stringify方法:
var person={"name":"zhangsan","sex":"男","age":"24"};//json對象 var personString = JSON.stringify(person); alert(personString);
SpringMVC接受json字符串類型
在SpringMVC中基于REST開發(fā)時,前端傳入后臺的應(yīng)該是一個json格式的字符串,而不是一個json對象
GET、POST方式提時, 根據(jù)request header Content-Type的值來判斷:
application/x-www-form-urlencoded, 可選(即非必須,因為這種情況的數(shù)據(jù)@RequestParam, @ModelAttribute也可以處理,當(dāng)然@RequestBody也能處理);
multipart/form-data, 不能處理(即使用@RequestBody不能處理這種格式的數(shù)據(jù));
其他格式, 必須(其他格式包括application/json, application/xml等。這些格式的數(shù)據(jù),必須使用@RequestBody來處理)。
@RequestBody 處理類型 和 對象 和 json相互轉(zhuǎn)換
1 @RequestBody 處理類型
在項目中經(jīng)常看到controller 中有 @RequestBody 字樣,他到底有什么作用?
一般使用表單提交數(shù)據(jù)時不需要使用@RequestBody 即可自動封裝數(shù)據(jù)到對應(yīng)的 Bean 中。@RequestBody 用來處理Content-Type: application/json, application/xml等
它是通過使用HandlerAdapter 配置的HttpMessageConverters來解析post data body,然后綁定到相應(yīng)的bean上的。
說明:使用 @RequestBody 解析數(shù)據(jù)需要添加 jackson 或 fastjson 依賴包。
maven 引入 fastjson 包
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.51</version> </dependency>
2 對象 和 json 相互轉(zhuǎn)換
在項目中經(jīng)常會遇到對象和 json 之間相互轉(zhuǎn)換,公共類 和 json 對象轉(zhuǎn)換, 靜態(tài)內(nèi)部類 和 json 對象轉(zhuǎn)換
2.1 沒有內(nèi)部類時 Student 類
@Data public class Student { private String id; private String name; private int age; private String sex; @Override public String toString() { return ToStringBuilder.reflectionToString(this); } }
json 和 對象 相互轉(zhuǎn)換
public static void main(String[] args) throws IOException { ObjectMapper mapper = new ObjectMapper(); Student student = new Student(); student.setName("good"); String s = mapper.writeValueAsString(student); System.out.println(s); Student hd2 = mapper.readValue(s, Student.class); System.out.println(hd2); }
2.2 有靜態(tài)內(nèi)部類時 Student 類
@Data public class Student { private String id; private String name; private int age; private String sex; private HomeData homeData; private BigDecimal salary; private String[] tel; @Override public String toString() { return ToStringBuilder.reflectionToString(this); } @Data public static class HomeData{ private Address address; @Override public String toString() { return ToStringBuilder.reflectionToString(this); } @Data public static class Address { private String country; private String city; @Override public String toString() { return ToStringBuilder.reflectionToString(this); } } } }
json 和 對象 之間相互轉(zhuǎn)換
public static void main(String[] args) throws IOException { ObjectMapper mapper = new ObjectMapper(); Student student = new Student(); Student.HomeData homeData = new Student.HomeData(); Student.HomeData.Address address = new Student.HomeData.Address(); address.setCountry("中國"); address.setCity("上海"); homeData.setAddress(address); student.setHomeData(homeData); String s = mapper.writeValueAsString(address); System.out.println(s); Student.HomeData.Address hd2 = mapper.readValue(s, Student.HomeData.Address.class); System.out.println(hd2); }
說明:主要方法有 mapper.writeValueAsString 和 mapper.readValue
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
java 地心坐標系(ECEF)和WGS-84坐標系(WGS84)互轉(zhuǎn)的實現(xiàn)
這篇文章主要介紹了java 地心坐標系(ECEF)和WGS-84坐標系(WGS84)互轉(zhuǎn)的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09Java8新特性Stream流中anyMatch和allMatch和noneMatch的區(qū)別解析
這篇文章主要介紹了Java8新特性Stream流中anyMatch和allMatch和noneMatch的區(qū)別解析,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2024-01-01mybatis同一張表多次連接查詢相同列賦值問題小結(jié)
這篇文章主要介紹了mybatis同一張表多次連接查詢相同列賦值問題,非常不錯,具有參考借鑒價值,需要的的朋友參考下2017-01-01Java單線程程序?qū)崿F(xiàn)實現(xiàn)簡單聊天功能
這篇文章主要介紹了Java單線程程序?qū)崿F(xiàn)實現(xiàn)簡單聊天功能,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-10-10java List去掉重復(fù)元素的幾種方式(小結(jié))
這篇文章主要介紹了java List去掉重復(fù)元素的幾種方式,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06springboot網(wǎng)站應(yīng)用使用第三方qq登錄的實現(xiàn)過程
這篇文章主要介紹了springboot網(wǎng)站應(yīng)用使用第三方qq登錄,本文通過實例圖文相結(jié)合給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-09-09idea創(chuàng)建SpringBoot項目時Type選maven?project和maven?pom有何區(qū)別
Maven是一個Java工程的管理工具,跟其相同功能的工具如Gradle,下面這篇文章主要給大家介紹了關(guān)于idea創(chuàng)建SpringBoot項目時Type選maven?project和maven?pom有何區(qū)別的相關(guān)資料,需要的朋友可以參考下2023-02-02