springmvc实现json交互 -requestBody和responseBody
json数据交互
1.为什么要进行json数据交互
json数据格式在接口调用中、html页面中较常用,json格式比较简单,解析还比较方便。
比如:webservice接口,传输json数据.
2.springmvc进行json交互
(1)请求json、输出json,要求请求的是json串,所以在前端页面中需要将请求的内容转成json,不太方便。
(2)请求key/value、输出json。此方法比较常用。
3.环境准备
3.1加载json转的jar包
springmvc中使用jackson的包进行json转换(@requestBody和@responseBody使用下边的包进行json转),如下:
jackson-core-asl-1.9.11.jar
jackson-mapper-asl-1.9.11.jar
@RequestBody作用:
@RequestBody注解用于读取http请求的内容(字符串),通过springmvc提供的HttpMessageConverter接口将读到的内容转换为json、xml等格式的数据并绑定到controller方法的参数上。
本例子应用:
@RequestBody注解实现接收http请求的json数据,将json数据转换为java对象
@ResponseBody作用:
该注解用于将Controller的方法返回的对象,通过HttpMessageConverter接口转换为指定格式的数据如:json,xml等,通过Response响应给客户端
本例子应用:
@ResponseBody注解实现将controller方法返回对象转换为json响应给客户端
3.2配置json转换器
在注解适配器中加入messageConverters
- <!--注解适配器 -->
- <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 /> 则不用定义上边的内容。
4.json交互测试
4.1输入json串,输出是json串
4.1.1jsp页面
使用jquery的ajax提交json串,对输出的json结果进行解析。
使用jduery别忘记引入jquery-1.4.4.min.js
- <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
- %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <base href="<%=basePath%>">
- <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 reuqestJson(){
- $.ajax({
- type:'post',
- url:'${pageContext.request.contextPath }/requestJson.action',
- contentType:'application/json;charset=utf-8',
- //数据格式是json串,商品信息
- data:'{"name":"手机","price":999}',
- success:function(data){//返回json结果
- alert(data);
- }
- });
- }
- </script>
- </head>
- <body>
- <input type="button" onclick="reuqestJson()" value="请求的是json,输出的是json"/>
- </body>
- </html>
4.1.2controller
- package cn.edu.hpu.ssm.controller;
- 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 cn.edu.hpu.ssm.po.ItemsCustom;
- //json交互测试
- @Controller
- public class JsonText {
- //请求json(商品信息),输出json(商品信息)
- //@RequestBody将请求的商品信息的json串转成itemsCustom对象
- //@ResponseBody将itemsCustom转成json格式输出
- @RequestMapping("/requestJson")
- public @ResponseBody ItemsCustom requestJson(@RequestBody ItemsCustom itemsCustom){
- //@ResponseBody将itemsCustom转成json格式输出
- return itemsCustom;
- }
- }
4.1.3测试结果
4.2输入key/value,输出是json串
4.2.1jsp页面
使用jquery的ajax提交key/value串,对输出的json结果进行解析。
- <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
- <%
- String path = request.getContextPath();
- String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
- %>
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
- <html>
- <head>
- <base href="<%=basePath%>">
- <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">
- //请求是key/value,输出是json
- function responseJson(){
- $.ajax({
- type:'post',
- url:'${pageContext.request.contextPath }/responseJson.action',
- //请求的是key/value,这里不需要指定contentType,因为默认就是key/value类型
- //contentType:'application/json;charset=utf-8',
- //数据格式是json串,商品信息
- data:'name=手机&price=999',
- success:function(data){//返回json结果
- alert(data);
- }
- });
- }
- </script>
- </head>
- <body>
- <input type="button" onclick="requestJson()" value="请求的是key/value,输出的是json"/>
- </body>
- </html>
4.2.2controller
- package cn.edu.hpu.ssm.controller;
- 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 cn.edu.hpu.ssm.po.ItemsCustom;
- //json交互测试
- @Controller
- public class JsonText {
- //请求key/value(商品信息),输出json(商品信息)
- @RequestMapping("/responseJson")
- public @ResponseBody ItemsCustom responseJson(ItemsCustom itemsCustom){
- //@ResponseBody将itemsCustom转成json格式输出
- System.out.println("前台传过来得商品名:"+itemsCustom.getName());
- return itemsCustom;
- }
- }
4.2.3测试
后台控制台输出了"前台传过来的商品名:手机",且查看http数据可以看到json数据的反馈。
转载请注明出处:http://www.lai18.com/content/505491.html
springmvc实现json交互 -requestBody和responseBody的更多相关文章
- SpringMVC之JSON交互
#什么是json? json是一种用于储存数据格式,是js脚本语言的子集. #json的作用? 它可以传递对象.数组等数据结构.如果是单个数据,则要用数组,不用对象,因为对象都是键值对的 方式去存储, ...
- AJAX发送json,SpringMVC 接收JSON,@RequestBody
需求:JQuery ajax前台,采用 POST请求 发送json,后台使用SpringMVC接收json并处理 前台: $.ajax({ url:"请求地址", type:&qu ...
- SpringMVC使用@PathVariable,@RequestBody,@ResponseBody,@RequestParam,@InitBinder
@Pathvariable public ResponseEntity<String> ordersBack( @PathVariable String reqKey, ...
- Ajax和SpringMVC之间JSON交互
Ajax和SpringMVC之间的json数据传输有两种方式: 1.直接传输Json对象 2.将Json序列化成json字符串 1.直接传输Json对象 前端Ajax $(document).read ...
- SpringMVC的json交互
一.注解说明 1.@RequestBody 作用:@RequestBody注解用于读取http请求的内容(字符串),通过springmvc提供的HttpMessageConverter接口将读到的内 ...
- springmvc之json交互406异常(Not Acceptable)和415异常(Unsupported Media Type)
一. 406异常(Not Acceptable) 1. 没有添加jackson-databind包2. 请求的url的后缀是*.html.在springmvc中如果请求的后缀是*.html的话,是不可 ...
- 九 SpringMvc与json交互
将json输出到页面: 1 加入jar包 2 配置Controller层,开启注解ResponseBody,将json发送到页面: 3 访问url 4 响应json,在形参列表里面加上注解
- SpringMVC学习--json
简介 json数据格式在接口调用中.html页面中较常用,json格式比较简单,解析还比较方便.比如:webservice接口,传输json数据. springmvc与json交互 @RequestB ...
- SpringMVC详解(六)------与json交互
Json(JavaScript Object Notation),它是一种轻量级数据交换格式,格式简单,易于读写,目前使用特别广泛.那么这篇博客我们主要谈谈在 SpringMVC 中,如何对 json ...
随机推荐
- poj-2514-模拟
http://poj.org/problem?id=2514 Ridiculous Addition Time Limit: 1000MS Memory Limit: 65536K Total S ...
- SQL Server跨服务器建立视图
create view View_AppCus as select dwmch,zjm from ksoa.dbo.mchk SQL Server跨服务器操作经常需要用到,下面就为你介绍的是SQL S ...
- ajax请求二进制流图片并渲染到html中img标签
日常显示图片都诸如这种形式:直接使用img的src属性 <img src="图片路径.地址" alt="" /> 以上方法无法在获取图片请求中设置请 ...
- [NOIP 2015TG D2T3] 运输计划
题目背景 公元 2044 年,人类进入了宇宙纪元. 题目描述 L 国有 n 个星球,还有 n-1 条双向航道,每条航道建立在两个星球之间,这 n-1 条航道连通了 L 国的所有星球. 小 P 掌管一家 ...
- java.sql.SQLException: Parameter index out of range (3 > number of parameters, which is 2).
java.sql.SQLException: Parameter index out of range (3 > number of parameters, which is 2). java. ...
- Oracle12c版本中未归档隐藏参数
In this post, I will give a list of all undocumented parameters in Oracle 12.1.0.1c. Here is a query ...
- Python模块和包使用
1.什么是模块 模块就是一个.py的文件 2.为什么要使用模块? 最开始的程序(没有任何组织)----> 函数------>类----->模块------>包 为了让程序的组 ...
- Struts 2 初步入门(六)之处理结果类型
Struts2 处理流程: 用户请求--->struts框架--->Action控制器--->struts框架--->视图资源 xml配置文件里: <result nam ...
- error: http://ppa.launchpad.net lucid Release: The following signatures couldn't be verified because
ubuntu 命令行sudo apt-get update W: GPG error: http://ppa.launchpad.net lucid Release: The following si ...
- http协商缓存VS强缓存
之前一直对浏览器缓存只能描述一个大概,深层次的原理不能描述上来:终于在前端的两次面试过程中被问倒下,为了泄恨,查阅一些资料最终对其有了一个更深入的理解,废话不多说,赶紧来看看浏览器缓存的那些事吧,有不 ...