SpringMVC JSON數(shù)據(jù)交互及RESTful支持實現(xiàn)方法
JSON概述
JSON(JavaScript Object Notation,JS對象標(biāo)記)是一種輕量級的數(shù)據(jù)交換格式,最近幾年才流行起來。JSON是基于JavaScript的一個子集,使用了C、C++、C#、Java、 JavaScript、Per、 Python等其他語言的約定,采用完全獨立于編程語言的文本格式來存儲和表示數(shù)據(jù)。這些特性使JSON成為理想的數(shù)據(jù)交互語言,它易于閱讀和編寫,同時也易于機器解析和生成。
與XML一樣,JSON也是基于純文本的數(shù)據(jù)格式。初學(xué)者可以使用JSON傳輸一個簡單的String、 Number、 Boolean,也可以傳輸一個數(shù)組或者一個復(fù)雜的 Object對象。
JSON有如下兩種數(shù)據(jù)結(jié)構(gòu)。
1.對象結(jié)構(gòu)
對象結(jié)構(gòu)以“{”開始,以“}”結(jié)束。中間部分由0個或多個以英文“,”分隔的“key:value”對構(gòu)成,其中key和value之間也是英語“:”。
{
keyl: valuel,
key2: value2,
……
}
2.數(shù)組結(jié)構(gòu)
數(shù)組結(jié)構(gòu)以“[”開始,以“]”結(jié)束。中間部分由0個或多個以英文“,”分隔的值的列表組成。
[ valuel, value2, …… ]
JSON數(shù)據(jù)轉(zhuǎn)換
為了實現(xiàn)瀏覽器與控制器類(Controller)之間的數(shù)據(jù)交互,Spring提供了一個HttpMessageConverter
Spring為 HttpMessageConverter
要使用MappingJacksona2HttpMessageConverter對數(shù)據(jù)進(jìn)行轉(zhuǎn)換,就需要使用 Jackson
的開源包,開發(fā)時所需的開源包及其描述如下所示。
- jackson-annoations-2.8. 8. Jar:JSON轉(zhuǎn)換注解包。
- jackson-core-2.8. 8.jar:JSON轉(zhuǎn)換核心包。
- Jackson- databind-2.8.8.jar:JSON轉(zhuǎn)換的數(shù)據(jù)綁定包。
在使用注解式開發(fā)時,需要用到兩個重要的JSON格式轉(zhuǎn)換注解@RequestBody和@ ResponseBody,


springmvc-config. xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!--指定需要掃描的包 -->
<context:component-scan base-package="com.ssm.controller" />
<!-- 配置注解驅(qū)動 -->
<mvc:annotation-driven />
<!-- 配置靜態(tài)資源的訪問映射,此配置中的文件,將不被前端控制器攔截 -->
<mvc:resources location="/js/" mapping="/js/**"></mvc:resources>
<!-- 定義視圖解析器 -->
<bean id="viewResoler"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 設(shè)置前綴 -->
<property name="prefix" value="/WEB-INF/jsp/" />
<!-- 設(shè)置后綴 -->
<property name="suffix" value=".jsp" />
</bean>
</beans>
不僅配置了組件掃描器和視圖解析器,還配置了 Spring MVC的注解驅(qū)動<mvc: annotation- driven/>和靜態(tài)資源訪問映射mvc:resources…/。其中<mvc: annotation- driven/>配置會自動注冊 RequestMappingHandlerMapping和 RequestMappingHandlerAdapter兩個Bean,并提供對讀寫XML和讀寫JSON等功能的支持。mvc:resources…/元素用于配置靜態(tài)資源的訪問路徑。由于在web.xml中配置的“/”會將頁面中引入的靜態(tài)文件也進(jìn)行攔截,而攔截后頁面中將找不到這些靜態(tài)資源文件,這樣就會引起頁面報錯。而增加了靜態(tài)資源的訪問映射配置后,程序會自動地去配置路徑下找靜態(tài)的內(nèi)容。
json.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>測試JSON交互</title>
<script type="text/javascript"
src="${pageContext.request.contextPath}/js/jquery-1.11.3.min.js">
</script>
<script type="text/javascript">
function testJson(){
//獲取輸入的客戶信息
var loginname=$("#loginname").val();
var password=$("#password").val();
$.ajax({
url:"${pageContext.request.contextPath}/testJson",
type:"post",
//data表示發(fā)送的數(shù)據(jù)
data:JSON.stringfy({loginname:loginname,password:password}),
// 定義發(fā)送請求的數(shù)據(jù)格式為JSON字符串
contentType:"application/json;charset=UTF-8",
//定義回調(diào)響應(yīng)的數(shù)據(jù)格式為JSON字符串,該屬性可以省略
dataType:"json",
//成功響應(yīng)的結(jié)果
success:function(data){
if(data!=null){
alert("您輸入的登錄名為:"+data.loginname+"密碼為:"+data.password);
}
}
});
}
</script>
</head>
<body>
<form>
登錄名:<input type="text" name="loginname" id="loginname" /> <br />
密碼:<input type="password" name="password" id="password" /> <br />
<input type="button" value="測試JSON交互" onclick="testJson()" />
</form>
</body>
</html>
在AJAX中包含了3個特別重要的屬性,其說明如下。
- data:即請求時攜帶的數(shù)據(jù),當(dāng)使用JSON格式時,要注意編寫規(guī)范。
- contentType:當(dāng)請求數(shù)據(jù)為JSON格式時,值必須為 application/json。
- dataType:當(dāng)響應(yīng)數(shù)據(jù)為JSON時,可以定義dataType屬性,并且值必須為json。其中
- dataType:"json"也可以省略不寫,頁面會自動識別響應(yīng)的數(shù)據(jù)格式。
- 在上述測試頁面 json.jsp還需要引入jquery.js文件,本例中引入了 WebContent目錄下js文件夾中的jquery-1.11.3.min.js。
CustomerController.java:
package com.ssm.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ssm.po.Customer;
@Controller
public class CustomerController {
/*
* 接收頁面請求的JSON數(shù)據(jù),并返回JSON格式結(jié)果
*/
@ResponseBody
public Customer testJson(@RequestBody Customer customer){
//打印接收到的JSON格式數(shù)據(jù)
System.out.println(customer);
return customer;
}
}
RESTful支持
RESTful也稱之為REST(Representational State Transfer),可以將它理解為一種軟件架構(gòu)風(fēng)格或設(shè)計風(fēng)格。
RESTful風(fēng)格就是把請求參數(shù)變成請求路徑的一種風(fēng)格。例如,傳統(tǒng)的URL請求格式為:
http://.../queryitems?id=1
而采用RESTful風(fēng)格后,其∪RL請求為:
http://.../items/1
/*
* 接收RESTful風(fēng)格的請求,其接收方式為GET
*/
@RequestMapping(value="/customer/{id}",method=RequestMethod.GET)
@ResponseBody
public Customer selectCustomer(@PathVariable("id") Integer id){
//查看接收數(shù)據(jù)
System.out.println(id);
Customer customer=new Customer();
//模擬根據(jù)id查詢出客戶對象數(shù)據(jù)
if(id==10){
customer.setLoginname("wujit");
}
//返回JSON格式的數(shù)據(jù)
return customer;
}
@RequestMapping(vaue="customer/{id}", method= RequestMethod.GET)注解用于匹配請求路徑(包括參數(shù))和方式。其中vaue="/user/{id}"表示可以匹配以“/user/{id}”結(jié)尾的請求,id為請求中的動態(tài)參數(shù);method= RequestMethod.GET表示只接收GET方式的請求。方法中的@ PathVariable("id")注解則用于接收并綁定請求參數(shù),它可以將請求URL中的變量映射到方法的形參上,如果請求路徑為“/user/{id}”,即請求參數(shù)中的id和方法形參名稱id一樣,則@PathVariable后面的“("id")”可以省略。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- SpringMVC?RESTFul及REST架構(gòu)風(fēng)格介紹
- SpringMVC?RESTFul實體類創(chuàng)建及環(huán)境搭建
- SpringMVC?RESTFul實戰(zhàn)案例訪問首頁
- SpringMVC?RESTFul實現(xiàn)列表功能
- SpringMVC?RESTFul實戰(zhàn)案例刪除功能實現(xiàn)
- SpringMVC?RESTFul實戰(zhàn)案例修改功能實現(xiàn)
- SpringMVC實戰(zhàn)案例RESTFul實現(xiàn)添加功能
- 關(guān)于SpringMVC對Restful風(fēng)格的支持詳解
- SpringMVC通過RESTful結(jié)構(gòu)實現(xiàn)頁面數(shù)據(jù)交互
相關(guān)文章
java?socket實現(xiàn)局域網(wǎng)聊天
這篇文章主要為大家詳細(xì)介紹了java?socket實現(xiàn)局域網(wǎng)聊天,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-05-05
Java實現(xiàn)斷點下載服務(wù)端與客戶端的示例代碼
這篇文章主要為大家介紹了如何實現(xiàn)服務(wù)端(Spring Boot)與客戶端(Android)的斷點下載與下載續(xù)傳功能,文中的示例代碼講解詳細(xì),需要的可以參考一下2022-08-08
使用java + selenium + OpenCV破解網(wǎng)易易盾滑動驗證碼的示例
這篇文章主要介紹了使用java + selenium + OpenCV破解網(wǎng)易易盾滑動驗證碼,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-02-02
Spring boot創(chuàng)建自定義starter的完整步驟
這篇文章主要給大家介紹了關(guān)于Spring boot創(chuàng)建自定義starter的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Spring boot具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09

