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

  1. <!--注解适配器 -->
  2. <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
  3. <property name="messageConverters">
  4. <list>
  5. <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
  6. </list>
  7. </property>
  8. </bean>

注意:如果使用<mvc:annotation-driven /> 则不用定义上边的内容。

4.json交互测试

4.1输入json串,输出是json串

4.1.1jsp页面

使用jquery的ajax提交json串,对输出的json结果进行解析。

使用jduery别忘记引入jquery-1.4.4.min.js

  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  7. <html>
  8. <head>
  9. <base href="<%=basePath%>">
  10. <title>json交互测试</title>
  11. <script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery-1.4.4.min.js"></script>
  12. <script type="text/javascript">
  13. //请求的是json,输出的是json
  14. function reuqestJson(){
  15. $.ajax({
  16. type:'post',
  17. url:'${pageContext.request.contextPath }/requestJson.action',
  18. contentType:'application/json;charset=utf-8',
  19. //数据格式是json串,商品信息
  20. data:'{"name":"手机","price":999}',
  21. success:function(data){//返回json结果
  22. alert(data);
  23. }
  24. });
  25. }
  26. </script>
  27. </head>
  28. <body>
  29. <input type="button" onclick="reuqestJson()"  value="请求的是json,输出的是json"/>
  30. </body>
  31. </html>

4.1.2controller

  1. package cn.edu.hpu.ssm.controller;
  2. import org.springframework.stereotype.Controller;
  3. import org.springframework.web.bind.annotation.RequestBody;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.ResponseBody;
  6. import cn.edu.hpu.ssm.po.ItemsCustom;
  7. //json交互测试
  8. @Controller
  9. public class JsonText {
  10. //请求json(商品信息),输出json(商品信息)
  11. //@RequestBody将请求的商品信息的json串转成itemsCustom对象
  12. //@ResponseBody将itemsCustom转成json格式输出
  13. @RequestMapping("/requestJson")
  14. public @ResponseBody ItemsCustom requestJson(@RequestBody ItemsCustom itemsCustom){
  15. //@ResponseBody将itemsCustom转成json格式输出
  16. return itemsCustom;
  17. }
  18. }

4.1.3测试结果

4.2输入key/value,输出是json串

4.2.1jsp页面

使用jquery的ajax提交key/value串,对输出的json结果进行解析。

  1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
  2. <%
  3. String path = request.getContextPath();
  4. String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
  5. %>
  6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  7. <html>
  8. <head>
  9. <base href="<%=basePath%>">
  10. <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" >
  11. <title>json交互测试</title>
  12. <script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery-1.4.4.min.js"></script>
  13. <script type="text/javascript">
  14. //请求是key/value,输出是json
  15. function responseJson(){
  16. $.ajax({
  17. type:'post',
  18. url:'${pageContext.request.contextPath }/responseJson.action',
  19. //请求的是key/value,这里不需要指定contentType,因为默认就是key/value类型
  20. //contentType:'application/json;charset=utf-8',
  21. //数据格式是json串,商品信息
  22. data:'name=手机&price=999',
  23. success:function(data){//返回json结果
  24. alert(data);
  25. }
  26. });
  27. }
  28. </script>
  29. </head>
  30. <body>
  31. <input type="button" onclick="requestJson()" value="请求的是key/value,输出的是json"/>
  32. </body>
  33. </html>

4.2.2controller

  1. package cn.edu.hpu.ssm.controller;
  2. import org.springframework.stereotype.Controller;
  3. import org.springframework.web.bind.annotation.RequestBody;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.ResponseBody;
  6. import cn.edu.hpu.ssm.po.ItemsCustom;
  7. //json交互测试
  8. @Controller
  9. public class JsonText {
  10. //请求key/value(商品信息),输出json(商品信息)
  11. @RequestMapping("/responseJson")
  12. public @ResponseBody ItemsCustom responseJson(ItemsCustom itemsCustom){
  13. //@ResponseBody将itemsCustom转成json格式输出
  14. System.out.println("前台传过来得商品名:"+itemsCustom.getName());
  15. return itemsCustom;
  16. }
  17. }

4.2.3测试

后台控制台输出了"前台传过来的商品名:手机",且查看http数据可以看到json数据的反馈。

转载请注明出处:http://www.lai18.com/content/505491.html

springmvc实现json交互 -requestBody和responseBody的更多相关文章

  1. SpringMVC之JSON交互

    #什么是json? json是一种用于储存数据格式,是js脚本语言的子集. #json的作用? 它可以传递对象.数组等数据结构.如果是单个数据,则要用数组,不用对象,因为对象都是键值对的 方式去存储, ...

  2. AJAX发送json,SpringMVC 接收JSON,@RequestBody

    需求:JQuery ajax前台,采用 POST请求 发送json,后台使用SpringMVC接收json并处理 前台: $.ajax({ url:"请求地址", type:&qu ...

  3. SpringMVC使用@PathVariable,@RequestBody,@ResponseBody,@RequestParam,@InitBinder

    @Pathvariable public ResponseEntity<String> ordersBack(           @PathVariable String reqKey, ...

  4. Ajax和SpringMVC之间JSON交互

    Ajax和SpringMVC之间的json数据传输有两种方式: 1.直接传输Json对象 2.将Json序列化成json字符串 1.直接传输Json对象 前端Ajax $(document).read ...

  5. SpringMVC的json交互

    一.注解说明 1.@RequestBody  作用:@RequestBody注解用于读取http请求的内容(字符串),通过springmvc提供的HttpMessageConverter接口将读到的内 ...

  6. springmvc之json交互406异常(Not Acceptable)和415异常(Unsupported Media Type)

    一. 406异常(Not Acceptable) 1. 没有添加jackson-databind包2. 请求的url的后缀是*.html.在springmvc中如果请求的后缀是*.html的话,是不可 ...

  7. 九 SpringMvc与json交互

    将json输出到页面: 1 加入jar包 2 配置Controller层,开启注解ResponseBody,将json发送到页面: 3 访问url 4 响应json,在形参列表里面加上注解

  8. SpringMVC学习--json

    简介 json数据格式在接口调用中.html页面中较常用,json格式比较简单,解析还比较方便.比如:webservice接口,传输json数据. springmvc与json交互 @RequestB ...

  9. SpringMVC详解(六)------与json交互

    Json(JavaScript Object Notation),它是一种轻量级数据交换格式,格式简单,易于读写,目前使用特别广泛.那么这篇博客我们主要谈谈在 SpringMVC 中,如何对 json ...

随机推荐

  1. poj-2514-模拟

    http://poj.org/problem?id=2514 Ridiculous Addition Time Limit: 1000MS   Memory Limit: 65536K Total S ...

  2. SQL Server跨服务器建立视图

    create view View_AppCus as select dwmch,zjm from ksoa.dbo.mchk SQL Server跨服务器操作经常需要用到,下面就为你介绍的是SQL S ...

  3. ajax请求二进制流图片并渲染到html中img标签

    日常显示图片都诸如这种形式:直接使用img的src属性 <img src="图片路径.地址" alt="" /> 以上方法无法在获取图片请求中设置请 ...

  4. [NOIP 2015TG D2T3] 运输计划

    题目背景 公元 2044 年,人类进入了宇宙纪元. 题目描述 L 国有 n 个星球,还有 n-1 条双向航道,每条航道建立在两个星球之间,这 n-1 条航道连通了 L 国的所有星球. 小 P 掌管一家 ...

  5. 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. ...

  6. Oracle12c版本中未归档隐藏参数

    In this post, I will give a list of all undocumented parameters in Oracle 12.1.0.1c. Here is a query ...

  7. Python模块和包使用

    1.什么是模块 模块就是一个.py的文件 2.为什么要使用模块? 最开始的程序(没有任何组织)----> 函数------>类----->模块------>包  为了让程序的组 ...

  8. Struts 2 初步入门(六)之处理结果类型

    Struts2 处理流程: 用户请求--->struts框架--->Action控制器--->struts框架--->视图资源 xml配置文件里: <result nam ...

  9. 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 ...

  10. http协商缓存VS强缓存

    之前一直对浏览器缓存只能描述一个大概,深层次的原理不能描述上来:终于在前端的两次面试过程中被问倒下,为了泄恨,查阅一些资料最终对其有了一个更深入的理解,废话不多说,赶紧来看看浏览器缓存的那些事吧,有不 ...