springMVC学习(11)-json数据交互和RESTful支持
一、json数据交互:
json数据格式在接口调用中、html页面中较常用,json格式比较简单,解析还比较方便。
比如:webservice接口,传输json数据.
springMVC进行json交互

1)环境准备:
加载json转换的jar包:
springmvc中使用jackson的包进行json转换(@requestBody和@responseBody使用下边的包进行json转)
jackson-core-asl-1.9.11.jar
jackson-mapper-asl-1.9.11.jar
2)配置json转换器;
如果是配置单个的注解适配器,要加入下面配置:
<!--注解适配器 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
</list>
</property>
</bean>
如果使用<mvc:annotation-driven /> 则不用定义上边的内容。
3)代码实现:测试:
package com.cy.controller; import org.springframework.beans.factory.annotation.Autowired;
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.cy.po.ItemsCustom;
import com.cy.service.ItemsService; /**
* json交互测试
*
*/
@Controller
public class JsonTest { @Autowired
private ItemsService itemsService; //请求json串(商品信息),输出json(商品信息)
//@RequestBody将请求的商品信息的json串转成itemsCustom对象
@RequestMapping("/requestJson")
@ResponseBody
public ItemsCustom requestJson(@RequestBody ItemsCustom itemsCustom) throws Exception{
int id = itemsCustom.getId();
ItemsCustom itemsCustom2 = itemsService.findItemsById(id);
return itemsCustom2;
} //请求key/value,输出json
//@ResponseBody将itemsCustom转成json输出
@RequestMapping("/responseJson")
@ResponseBody
public ItemsCustom responseJson(ItemsCustom itemsCustom) throws Exception{
int id = itemsCustom.getId();
ItemsCustom itemsCustom2 = itemsService.findItemsById(id);
return itemsCustom2;
}
}
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.4.4.min.js"></script>
<script type="text/javascript">
//请求json,输出是json
function requestJson(){
$.ajax({
type:'post',
url:'${pageContext.request.contextPath }/requestJson.action',
contentType:'application/json;charset=utf-8',
//数据格式是json串,商品信息
//这边很严格的做了测试,必须是这种格式;
//key加引号;整个{}加引号;整个就是一个json串;有点像JOSN.stringify(JOSNObject)这样子
data:'{"id":1,"name":"手机","price":999}',
success:function(data){
console.log(data);
} });
}
//请求key/value,输出是json
function responseJson(){
$.ajax({
type:'post',
url:'${pageContext.request.contextPath }/responseJson.action',
data:'id=4&name=手机&price=999',
success:function(data){//返回json结果
console.log(data);
}
});
}
</script>
</head>
<body>
<input type="button" onclick="requestJson()" value="请求json,输出是json"/>
<input type="button" onclick="responseJson()" value="请求key/value,输出是json"/>
</body>
</html>
测试结果:
数据库内容:

requestJson:

responseJson:

二、RESTful支持:
RESTful介绍:
RESTful(即Representational State Transfer的缩写)其实是一个开发理念,是对http的很好的诠释。
1、对url进行规范,写RESTful格式的url
非REST的url:http://...../queryItems.action?id=001&type=T01
REST的url风格:http://..../items/001
特点:url简洁,将参数通过url传到服务端
2、http的方法规范
不管是删除、添加、更新。。使用url是一致的,如果进行删除,需要设置http的方法为delete,同理添加put。。。
后台controller方法:判断http方法,如果是delete执行删除,如果是post执行更新。
3、对http的contentType规范
请求时指定contentType,要json数据,设置成json格式的type。。
下面是RESTful的例子:这个例子主要是对url进行了规范:
1)ItemsController:
定义方法,进行url映射使用REST风格的url,将查询商品信息的id传入controller .
@Controller
@RequestMapping("/items")
public class ItemsController { @Autowired
private ItemsService itemsService; //查询商品信息,输出json
///itemsView/{id}里边的{id}表示占位符,通过@PathVariable获取占位符中的参数,
//如果占位符中的名称和形参名一致,在@PathVariable可以不指定名称
@RequestMapping("/itemsView/{id}")
@ResponseBody
public ItemsCustom itemsView(@PathVariable("id") Integer itemId)throws Exception{
ItemsCustom itemsCustom = itemsService.findItemsById(itemId);
return itemsCustom;
} ....
}
2)REST风格的前端控制器配置、静态资源文件配置:
由于REST风格是url是不带后缀的,这里配置url-pattern为/,亦需要配置静态文件不让dispatcherServlert解析:
web.xml中配置:
<!-- springmvc前端控制器,rest配置 -->
<servlet>
<servlet-name>springmvc_rest</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc_rest</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
springmvc中配置访问静态资源:
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:resources mapping="/js/**" location="/js/"/>
访问结果:

springMVC学习(11)-json数据交互和RESTful支持的更多相关文章
- 1.4(Spring MVC学习笔记)JSON数据交互与RESTful支持
一.JSON数据交互 1.1JSON简介 JSON(JavaScript Object Notation)是一种数据交换格式. 1.2JSON对象结构 {}代表一个对象,{}中写入数据信息,通常为ke ...
- Spring MVC之JSON数据交互和RESTful的支持
1.JSON概述 1.1 什么是JSON JSON(JavaScript Object Notation,JS对象标记)是一种轻量级的数据交换格式.它是基于JavaScript的一个子集,使用了C.C ...
- SprimgMVC学习笔记(八)—— SpringMVC与前台json数据交互
一.两种交互形式 可以看出,前台传过来的方式有两种,一种是传json格式的数据过来,另一种就是在url的末尾传普通的key/value串过来,针对这两种方式,在Controller类中会有不同的解析, ...
- SpringBoot学习之Json数据交互
最近在弄监控主机项目,对javaweb又再努力学习.实际的项目场景中,前后分离几乎是所以项目的标配,全栈的时代的逐渐远去,后端负责业务逻辑处理,前端负责数据展示成了一种固定的开发模式.像thymele ...
- springmvc学习笔记(18)-json数据交互
springmvc学习笔记(18)-json数据交互 标签: springmvc springmvc学习笔记18-json数据交互 springmvc进行json交互 环境准备 加入json转换的依赖 ...
- spring-boot json数据交互
SpringBoot学习之Json数据交互 最近在弄监控主机项目,对javaweb又再努力学习.实际的项目场景中,前后分离几乎是所以项目的标配,全栈的时代的逐渐远去,后端负责业务逻辑处理,前端负责数据 ...
- 【SpringMVC学习09】SpringMVC与前台的json数据交互
json数据格式在接口调用中.html页面中比较常用,json格式比较简单,解析也比较方便,所以使用很普遍.在springmvc中,也支持对json数据的解析和转换,这篇文章主要总结一下springm ...
- (转)SpringMVC学习(十)——SpringMVC与前台的json数据交互
http://blog.csdn.net/yerenyuan_pku/article/details/72514022 json数据格式在接口调用中.html页面中比较常用,json格式比较简单,解析 ...
- SpringMVC JSON数据交互
本节内容: @RequestBody @ResponseBody 请求json,响应json实现 前端可以有很多语言来写,但是基本上后台都是java开发的,除了c++(开发周期长),PHP和#Net( ...
随机推荐
- Mysql Innodb 表碎片整理
一.为什么会产生碎片 简单的说,删除数据必然会在数据文件中造成不连续的空白空间,而当插入数据时,这些空白空间则会被利用起来.于是造成了数据的存储位置不连续,以及物理存储顺序与理论上的排序顺序不同,这种 ...
- delphi中使用MSWINSCK.OCX控件
1.首先是把winsck控件导入到delphi中,就是导入一个ActiveX控件,步骤略过. 2.将导入的winsck控件拖入你的Form中. 3.对winsck进行基本设置(IP,Port). 4. ...
- TreeView添加treeView1_NodeMouseClick----多么痛的领悟。。。
TreeView添加treeView1_NodeMouseClick----多么痛的领悟... 1首先说一点,通过参考代码,已经实现了菜单项自动添加到TreeView控件的树视图了. 2.在移植(菜单 ...
- linux 优化git操作速度
修改 ssh配置:useDNS:no
- UI基础:UIControl及其子类
UISegmentedControl UISegmentedControl 是iOS中的分段控件 每个segment 都能被点击,相当于集成了若干个button. 通常我们会点击不同的segment ...
- Webform---母版页(Master Pages)
母版页(Master Pages)为网站内的其他页面提供模版. Master Page 使您有能力为 web 应用程序中的所有页面(或页面组)创建一致的外观和行为. Master Page 为其他页面 ...
- 利用GPU改善程序性能的一点心得
1. 硬件方面 a. 流处理器个数 Gpu内部的计算单元个数,决定分析模块实时性的关键因素. 实测效果: gtx760 1152个 Gtx960 1024个 单路1080p运动 ...
- ECHO不换行
我想用批处理实现向s.txt中多次分别导入文本例如:“aaaa","bbbb","cccc","dddd"实现s.txt内效果如: ...
- Java 发展历史
Java自1995诞生,至今已经20多年的历史. Java的名字的来源:Java是印度尼西亚爪哇岛的英文名称,因盛产咖啡而闻名.Java语言中的许多库类名称,多与咖啡有关,如JavaBeans(咖啡豆 ...
- 3d之ui快速切换图像
Requirement canon相机continuous mode(Burst mode) 抓图variation (230~320ms) 1. python + opencv 用cvWaitKey ...