SpringBoot處理JSON數(shù)據(jù)方法詳解
前言
在Spring Boot的Web應(yīng)用中 內(nèi)置了JSON數(shù)據(jù)的解析功能,默認(rèn)使用Jackson自動完成解析(不需要解析加載Jackson依賴包)當(dāng)控制器返回一個Java對象或集合數(shù)據(jù)時 Spring Boot自動將其轉(zhuǎn)換成JSON數(shù)據(jù),使用起來很方便簡潔。
Spring Boot處理JSON數(shù)據(jù)時,需要用到兩個重要的JSON格式轉(zhuǎn)換注解,分別是@RquestBody
@ResponseBody 他們的作用分別如下
@RequestBody:用于將請求體中的數(shù)據(jù)綁定到方法的形參中,該注解應(yīng)用在方法的形參上
@ResponseBody:用于直接返回JSON對象 該注解應(yīng)用在方法上
下面通過一個實例講解Spring Boot處理JSON數(shù)據(jù)的過程,該實例針對返回實體對象,ArrayList集合,Map<String,Object>集合以及List<Map<String,Object>>集合分別處理
步驟如下
1、創(chuàng)建實體類
在應(yīng)用的com.ch.ch5_2.model包中 創(chuàng)建實體類Preson 代碼如下
package com.ch.ch5_2.model; public class Preson { private String pname; private String password; private Integer page; public String getPname() { return pname; } public void setPname(String pname) { this.pname = pname; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Integer getPage() { return page; } public void setPage(Integer page) { this.page = page; } }
2、創(chuàng)建視圖頁面
在src/main/resources/templates目錄下 創(chuàng)建視圖頁面 input.html 并且引入jQuery框架 并使用它的ajax方法進(jìn)行異步請求 部分代碼如下(此處需要一些Java Web開發(fā)的知識 如有不明白的可以參考我之前的博客 進(jìn)主頁就有)
<!DOCTYPE html> <html xmlns:th="http://www.thymeleaf.org"> <head> <meta charset="UTF-8"> <title>Insert title here</title> <link rel="stylesheet" th:href="@{css/bootstrap.min.css}" rel="external nofollow" /> <!-- 默認(rèn)訪問 src/main/resources/static下的css文件夾--> <link rel="stylesheet" th:href="@{css/bootstrap-theme.min.css}" rel="external nofollow" /> <!-- 引入jQuery --> <script type="text/javascript" th:src="@{js/jquery.min.js}"></script> <script type="text/javascript"> function testJson() { //獲取輸入的值pname為id var pname = $("#pname").val(); var password = $("#password").val(); var page = $("#page").val(); alert(password); $.ajax({ //發(fā)送請求的URL字符串 url : "testJson", //定義回調(diào)響應(yīng)的數(shù)據(jù)格式為JSON字符串,該屬性可以省略 dataType : "json", //請求類型 type : "post", //定義發(fā)送請求的數(shù)據(jù)格式為JSON字符串 contentType : "application/json", //data表示發(fā)送的數(shù)據(jù) data : JSON.stringify({pname:pname,password:password,page:page}), //成功響應(yīng)的結(jié)果 success : function(data){ if(data != null){ //返回一個Person對象 //alert("輸入的用戶名:" + data.pname + ",密碼:" + data.password + ",年齡:" + data.page); //ArrayList<Person>對象 /**for(var i = 0; i < data.length; i++){ alert(data[i].pname); }**/ //返回一個Map<String, Object>對象 //alert(data.pname);//pname為key //返回一個List<Map<String, Object>>對象 for(var i = 0; i < data.length; i++){ alert(data[i].pname); } } }, //請求出錯 error:function(){ alert("數(shù)據(jù)發(fā)送失敗"); } }); } </script> </head> hicon-pencil"></i> </span> <input class="form-control" type="text" id="pname" th:placeholder="請輸入用戶名"/> </div> </div> <div class="form-group"> <div class="input-group col-md-6"> <span class="input-group-addon"> <i class="glyphicon glyphicon-pencil"></i> </span> <input class="form-control" type="text" id="password" th:placeholder="請輸入密碼"/> </div> </div> <div class="form-group"> <div class="input-group col-md-6"> <span class="input-group-addon"> <i class="glyphicon glyphicon-pencil"></i> </span> <input class="form-control" type="text" id="page" th:placeholder="請輸入年齡"/> </div> </div> <div class="form-group"> <div class="col-md-6"> <div class="btn-group btn-group-justified"> <div class="btn-group"> <button type="button" onclick="testJson()" class="btn btn-success"> <span class="glyphicon glyphicon-share"></span> 測試 </button> </div> </div> </div> </div> </form> </div> </div> </div> </body> </html>
3、創(chuàng)建控制器
在ch5_2應(yīng)用的com.ch.ch5_2.controller包中 創(chuàng)建控制器類TestJsonController 在該類中有兩個處理方法,一個時界面導(dǎo)航方法,一個是接受頁面請求的方法 部分代碼如下
package com.test.ch3_2.controller; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import com.ch.ch5_2.model.Preson; @Controller public class TestJsonController { /** * 進(jìn)入視圖頁面 */ @RequestMapping("/input") public String input() { return "input"; } /** * 接收頁面請求的JSON數(shù)據(jù) */ @RequestMapping("/testJson") @ResponseBody /*@RestController注解相當(dāng)于@ResponseBody + @Controller合在一起的作用。 1) 如果只是使用@RestController注解Controller,則Controller中的方法無法返回jsp頁面或者h(yuǎn)tml, 返回的內(nèi)容就是Return的內(nèi)容。 2) 如果需要返回到指定頁面,則需要用 @Controller注解。如果需要返回JSON,XML或自定義mediaType內(nèi)容到頁面, 則需要在對應(yīng)的方法上加上@ResponseBody注解。 */ public List<Map<String, Object>> testJson(@RequestBody Preson user) { //打印接收的JSON格式數(shù)據(jù) System.out.println("pname=" + user.getPname() + ", password=" + user.getPassword() + ",page=" + user.getPage()); //返回Person對象 Map<String, Object> map1 = new HashMap<String, Object>(); map1.put("pname", "陳恒3"); map1.put("password", "54321"); map1.put("page", 55); allp.add(map1); return allp; } }
然后運行Ch52Application主類 然后訪問http://localhost:8080/ch5_2/input
運行效果如下
到此這篇關(guān)于SpringBoot處理JSON數(shù)據(jù)方法詳解的文章就介紹到這了,更多相關(guān)SpringBoot JSON數(shù)據(jù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring Data JPA的Audit功能審計數(shù)據(jù)庫的變更
數(shù)據(jù)庫審計是指當(dāng)數(shù)據(jù)庫有記錄變更時,可以記錄數(shù)據(jù)庫的變更時間和變更人等,這樣以后出問題回溯問責(zé)也比較方便,本文討論Spring Data JPA審計數(shù)據(jù)庫變更問題,感興趣的朋友一起看看吧2021-06-06java數(shù)據(jù)結(jié)構(gòu)之樹基本概念解析及代碼示例
這篇文章主要介紹了java數(shù)據(jù)結(jié)構(gòu)之樹基本概念解析及代碼示例,介紹了樹的定義,基本術(shù)語,主要操作及實現(xiàn)等相關(guān)內(nèi)容,具有一定參考價值,需要的朋友可了解下。2017-11-11圖解Java經(jīng)典算法希爾排序的原理與實現(xiàn)
希爾排序是希爾(Donald Shell)于1959年提出的一種排序算法。希爾排序也是一種插入排序,它是簡單插入排序經(jīng)過改進(jìn)之后的一個更高效的版本,也稱為縮小增量排序,同時該算法是沖破O(n2)的第一批算法之一。本文會以圖解的方式詳細(xì)介紹希爾排序的基本思想及其代碼實現(xiàn)2022-09-09SpringBoot定時任務(wù)動態(tài)擴(kuò)展ScheduledTaskRegistrar詳解
這篇文章主要為大家介紹了SpringBoot定時任務(wù)動態(tài)擴(kuò)展ScheduledTaskRegistrar類示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01Java編程rabbitMQ實現(xiàn)消息的收發(fā)
RabbitMQ是一個在AMQP基礎(chǔ)上完成的,可復(fù)用的企業(yè)消息系統(tǒng),本文通過實例來給大家分享通過操作rabbitMQ實現(xiàn)消息的收發(fā),感興趣的朋友可以參考下。2017-09-09詳解Spring boot Admin 使用eureka監(jiān)控服務(wù)
本篇文章主要介紹了詳解Spring boot Admin 使用eureka監(jiān)控服務(wù),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12springboot實現(xiàn)多實例crontab搶占定時任務(wù)(實例代碼)
這篇文章主要介紹了springboot實現(xiàn)多實例crontab搶占定時任務(wù),本文通過實例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2020-01-01Mybatis查詢返回Map<String,Object>類型實例詳解
這篇文章主要給大家介紹了關(guān)于Mybatis查詢返回Map<String,Object>類型的相關(guān)資料,平時沒太注意怎么用,今天又遇到了總結(jié)記錄一下,方便以后處理此類問題,需要的朋友可以參考下2022-07-07