#什么是json?

json是一种用于储存数据格式或是数据传输方式,是js脚本语言的子集。

#json的作用?

传递对象、数组等数据结构。如果是单个数据,则要用数组,不用对象,因为对象是键值对的方式去存储,其形式为“名称:值”的形式,名称必须为字符串类型,而可以为任意类型

json是用于数据交换的,浏览器与控制器之间进行数据交换,Spring提供了一个接口来完成该工作,并用该接口的实现类来完成操作,该实现类要用jackson开源包(在Maven仓库中找到相关依赖,然后在pom.xml中下载相关的数据)来读写数据,并将java对象转换为json对象或xml文档,也可以将json对象和xml文档转换为java对象。(java对象<----->json对象或xml文档进行转换)

#json与xml区别?

都是数据传输方式,但相比之下,json占内存小,且解析速度快

-------------------------------------------------------------------------------------

#操作:

1).添加相关Spring包,另外,在http://mvnrepository.com/artifact/com.fasterxml.jackson.core下载fastjson包,其实在github上有相关代码备份;从经验上来看最好用阿里的fastjson,用json包有时会出现版本冲突;

相关的pom.xml依赖入下:

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.4.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.24</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
</dependencies>

2).在web.xml中配置前端控制器和Spring-config.xml的路径,并添加静态资源文件的配置,因web.xml没有设置对相关请求的拦截,需要在Spring-config.mvc中添加<mvc:resources location="" mapper="">,这里首先要引入jq库;

3).在Spring-config.xml中,添加额外如下配置:

 <mvc:annotation-driven>
<!--配置@ResponseBody由fastjson解析-->
<mvc:message-converters>
<bean class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"/>
</mvc:message-converters>
</mvc:annotation-driven>
<mvc:default-servlet-handler /> <!-- 支持mvc注解驱动 -->
<!--
在spring中一般采用@RequestMapping注解来完成映射关系,
要想使@RequestMapping注解生效必须向上下文中注册
DefaultAnnotationHandlerMapping
和一个AnnotationMethodHandlerAdapter
实例,这两个实例分别在类级别和方法级别处理。而annotation-driven配置帮助
我们自动完成上述两个实例的注入。
-->
<mvc:resources location="/js/" mapping="/js/**"/>

4).根据表单写javaBean;

5).写jsp页面,这里面涉及到ajax

 <%--
Created by IntelliJ IDEA.
User: shijinglu
Date: 2019/2/1
Time: 20:21
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>测试JSON交互</title> <!--引入js文件,引入路径 从当前项目的js文件夹下的jquery-1.11.3.min.js-->
<script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-1.11.3.min.js">
</script>
<!--写一个function testJson()-->
<script type="text/javascript">
function testJson() {
<!--获取用户输入的用户名和密码-->
var username=$("#username").val();
var password=$("#password").val();
<!--发一个ajax请求-->
$.ajax({
<!--testJson路径 默认是当前页的地址,这里是交给testJson处理-->
url:"${pageContext.request.contextPath}/testJson",
type:"post",
<!--把发送过来的数据转换成json字符串-->
data:JSON.stringify({username:username,password:password}),
<!--请求类型设置为json字符串,并设置字符集为utf-8-->
contentType: "application/json;charset=UTF-8",
<!--定义回调的响应格式为json字符串,该属性可以省略-->
dataType:"json",
success : function(data) {
if(data !=null){
alert("您输入的用户名为:"+data.username+"密码为:"+data.password);
}
}
}); }
</script>
</head>
<body>
<form>
用户名:<input name="username" id="username"><br/> <!--name后面的值必须与pojo里面封装的值一致,否则读取的值是空值-->
密&nbsp;&nbsp;&nbsp;码:<input type="password" name="password" id="password"><br/>
<input type="button" value="测试json交互" onclick="testJson()">
</form>
</body>
</html>

6).写控制器;

@Controller  //表示控制器  等价于在配置文件里面写<bean id="" class="">
public class UserController{ /**
* 接收页面请求的数据,并以json格式返回数据
* */
@RequestMapping(value="/testJson",method = RequestMethod.POST,consumes = "application/json")
//@ReponseBody:是把user对象转换为json,因为响应端的浏览器不认识java对象,但认识js对象标记。
@ResponseBody
//@RequestBody:把请求的json字符串(data后面的内容)转换成user对象
public User testJson(@RequestBody User user){
System.out.println(user);
//并返回json格式
return user;
}
}
 

原理待补充;

SpringMVC之JSON交互的更多相关文章

  1. springmvc实现json交互 -requestBody和responseBody

    json数据交互 1.为什么要进行json数据交互 json数据格式在接口调用中.html页面中较常用,json格式比较简单,解析还比较方便. 比如:webservice接口,传输json数据. 2. ...

  2. Ajax和SpringMVC之间JSON交互

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

  3. SpringMVC的json交互

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

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

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

  5. 九 SpringMvc与json交互

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

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

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

  7. SpringMVC学习--json

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

  8. SpringMVC框架五:图片上传与JSON交互

    在正式图片上传之前,先处理一个细节问题: 每一次发布项目,Tomcat都会重新解压war包,之前上传过的图片会丢失 为了解决这个问题:可以不在Tomcat下保存图片,而是另找一个目录. 上传图片: & ...

  9. springMVC的高级数据绑定,以及json交互,全局异常配置,

    一.窄化请求映射 1.在class上添加@RequestMapping(url)指定通用请求前缀, 限制此类下的所有方法请求url必须以请求前缀开头,通过此方法对url进行分类管理. 如下: @Con ...

随机推荐

  1. Spark Sql之ThriftServer和Beeline的使用

    概述 ThriftServer相当于service层,而ThriftServer通过Beeline来连接数据库.客户端用于连接JDBC的Server的一个工具 步骤 1:启动metastore服务 . ...

  2. [vue]模拟移动端三级路由: router-link位置体现router的灵活性

    小结 router-link可以随便放 router-view显示的是父组件的直接子组件的内容 想研究下移动三级路由的逻辑, 即 router-link和router-view 点首页--点新闻资讯( ...

  3. np.Linear algebra学习

    转自:https://docs.scipy.org/doc/numpy-1.13.0/reference/routines.linalg.html 1.分解 //其中我觉得可以的就是svd奇异值分解吧 ...

  4. NetBeans issues and solutions.(build.xml and debug multiple projects)

    Copy a directory to another directory when building the .jar in NetBeans in the build.xml file. Solu ...

  5. vim 常用

    Format JSON :%!python -m json.tool 1. define custom function and use it define function in .vimrc fu ...

  6. 委托 匿名 lambda表达式

    #region 委托 delegate int ACT(int a, int b); static void Main(string[] args) { ACT act = new ACT(add); ...

  7. Node.JS 项目打包 JXCore

    哈哈,又回来了 当你开发完成了Node.JS项目,你应该需要打包发行吧 好,JXCore就是干这个的啦! 嗯,可能你会这样来安装 1. curl http://jxcore.com/xil.sh | ...

  8. PHP 函数 ignore_user_abort()详解笔记

     定义和用法 ignore_user_abort()函数设置与客户机断开是否会终止脚本的执行  语法 ignore_user_abort(setting) 参数 描述 setting 可选.如果设置为 ...

  9. 《linux就该这么学》找到一本不错的Linux电子书,《Linux就该这么学》。

    本帖不是广告贴,只是感觉有好的工具书而已 本书是由全国多名红帽架构师(RHCA)基于最新Linux系统共同编写的高质量Linux技术自学教程,极其适合用于Linux技术入门教程或讲课辅助教材,目前是国 ...

  10. (4)Python3笔记 之 流程控制

    一.条件控制 # 语法规则 if 变量(或表达式): 语句块1 elif 变量(或表达式): 语句块2 else: 语句块3 #示例 score = 83 if score > 90: prin ...