springMVC 处理json 及 HttpMessageConverter 接口
一、SpringMVC处理json的使用
1、添加依赖jar包
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.8.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.7</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.7</version>
</dependency>
2、编写目标方法,使其返回 JSON 对应的对象或集合;
3、在方法上添加 @ResponseBody 注解:
@Controller
@RequestMapping("/user")
public class UserController
{
@Autowired
private UserService userService; @RequestMapping("/getUserList")
@ResponseBody
public Map<String, Object> getUserList(Model model)
{
List<User> userlist = userService.getUserList();
Map<String, Object> map = new HashMap<>();
map.put("aaData", userlist);
return map;
}
}
二、HttpMessageConverter
1、HttpMessageConverter信息
HttpMessageConverter<T> 是 Spring3.0 新添加的一个接口,负责将请求信息转换为一个对象(类型为 T),将对象(类型为 T)输出为响应信息

2、HttpMessageConverter 运行原理

(1)HttpInputMessage 将请求的信息先转为 InputStream 对象,InputStream 再由 HttpMessageConverter 转换为 SpringMVC 需要的java对象;
(2)SpringMVC 返回一个 java 对象, 并通过 HttpMessageConverter 转为响应信息,接着 HttpOutputMessage 将响应的信息转换为 OutputStream,接着给出响应。
3、 HttpMessageConveter 的实现类

4、 SpringMVC 默认加载的 HttpMessageConverter 实现类
(1)DispatcherServlet 默认加载 HttpMessageConveter 的个实现类,如下所示:

(2)加入 jackson jar包后,启动的时候加载个 HttpMessageConverter 的实现类,如下所示:

5、使用 HttpMessageConverter

(1) @ResponseBody 和 @RequestBody 示例:

(2) HttpEntity 和 ResponseEntity 示例


springMVC 处理json 及 HttpMessageConverter 接口的更多相关文章
- SpringMVC——处理 JSON:使用 HttpMessageConverter
一.SpringMVC处理JSON流程 1. 加入 jar 包: jackson-annotations-2.1.5.jarjackson-core-2.1.5.jarjackson-databind ...
- SpringMVC学习--json
简介 json数据格式在接口调用中.html页面中较常用,json格式比较简单,解析还比较方便.比如:webservice接口,传输json数据. springmvc与json交互 @RequestB ...
- SpringMVC关于json、xml自动转换的原理研究[附带源码分析]
目录 前言 现象 源码分析 实例讲解 关于配置 总结 参考资料 前言 SpringMVC是目前主流的Web MVC框架之一. 如果有同学对它不熟悉,那么请参考它的入门blog:http://www.c ...
- SpringMVC关于json、xml自动转换的原理研究
SpringMVC是目前主流的Web MVC框架之一. 如果有同学对它不熟悉,那么请参考它的入门blog:http://www.cnblogs.com/fangjian0423/p/springMVC ...
- SpringMVC关于json、xml自动转换的原理研究[附带源码分析 --转
SpringMVC关于json.xml自动转换的原理研究[附带源码分析] 原文地址:http://www.cnblogs.com/fangjian0423/p/springMVC-xml-json-c ...
- SpringMVC中Json数据格式转换
1 @RequestBody 作用: @RequestBody注解用于读取http请求的内容(字符串),通过springmvc提供的HttpMessageConverter接口将读到的内容转换为 ...
- SpringMVC的JSON数据交互(七)-@Response,@RestController,@RequestBody用法
1.@RequestBody (自动将请求的数据封装为对象) 作用: @RequestBody注解用于读取http请求的内容(字符串),通过springmvc提供的HttpMessageConve ...
- springmvc实现json交互 -requestBody和responseBody
json数据交互 1.为什么要进行json数据交互 json数据格式在接口调用中.html页面中较常用,json格式比较简单,解析还比较方便. 比如:webservice接口,传输json数据. 2. ...
- SpringMVC关于json、xml自动转换的原理研究[附带源码分析](使用JAXB转换XML)
前言 SpringMVC是目前主流的Web MVC框架之一. 如果有同学对它不熟悉,那么请参考它的入门blog:http://www.cnblogs.com/fangjian0423/p/spring ...
随机推荐
- CPU与内存互联的架构演变
随着计算机中CPU核数目的增加,传统的UMA(unifonn memory access)架构由于对关键硬件(如中央内存控制器)的竞争加剧出现了性能上的瓶颈,即扩展性不强.而NUMA架构则以其良好的可 ...
- 4-29 c语言之栈,队列,双向链表
今天学习了数据结构中栈,队列的知识 相对于单链表来说,栈和队列就是添加的方式不同,队列就相当于排队,先排队的先出来(FIFO),而栈就相当于弹夹,先压进去的子弹后出来(FILO). 首先看一下栈(St ...
- 模拟器 Unable to execute simctl install Error 117
ios 模拟器 Unable to execute '"/usr/bin/xcrun" simctl install "/Users/tt/PAServer/scrat ...
- install oracle 12c on redhat
---恢复内容开始--- 1. 确定VM的硬盘空间是否够 df- h, 硬盘空间free disk 15G 比较稳妥 2. 确定好网络,需要remote connect ifconfig 3. ...
- Linux简单学习
参考自:http://www.runoob.com/linux/linux-tutorial.html 一.是什么 Linux 类Unix操作系统.是一个基于POSIX和UNIX的多用户.多任务.支 ...
- 测试Linux下tcp最大连接数限制
现在做服务器开发不加上高并发根本没脸出门,所以为了以后吹水被别人怼“天天提高并发,你自己实现的最高并发是多少”的时候能义正言辞的怼回去,趁着元旦在家没事决定自己写个demo搞一搞. 这个测试主要是想搞 ...
- 题目--oil Deposits(油田) 基础DFS(深度搜索)
上一次基本了解了下BFS,这次又找了个基本的DFS题目来试试水,DFS举个例子来说就是 一种从树的最左端开始一直搜索到最底端,然后回到原端再搜索另一个位置到最底端,也就是称为深度搜索的DFS--dep ...
- 442. Find All Duplicates in an Array找出数组中所有重复了两次的元素
[抄题]: Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and o ...
- 716. Max Stack实现一个最大stack
[抄题]: Design a max stack that supports push, pop, top, peekMax and popMax. push(x) -- Push element x ...
- Vmware unknow Interface ens33
vmare打开虚拟网络编辑器,按图示操作