SpringMVC——处理 JSON:使用 HttpMessageConverter
一、SpringMVC处理JSON流程
1. 加入 jar 包:
jackson-annotations-2.1.5.jar
jackson-core-2.1.5.jar
jackson-databind-2.1.5.jar
2. 编写目标方法,使其返回 JSON 对应的对象或集合
3.在方法上添加 @ResponseBody 注解
/**@ResponseBody 输出处理(json,string) 将内容或对象作为 HTTP 响应正文返回,使用@ResponseBody将会跳过视图处理部分,而是调用适合HttpMessageConverter,将返回值写入输出流。
* 测试返回Json串
* @param map
* @return
*/
@ResponseBody
@RequestMapping("/testJson")
public List<Employee> testJson(Map<String ,Object> map){
return employeeService.getAllEmps();
}
4.页面Js
<script type="text/javascript">
$(function(){
$("#testjson").click(function(){
var url = this.href;
var args = {};
$.post(url,args,function(date){
for(var i=0;i<=date.length;i++){
var id = date[i].id;
var lastName = date[i].lastName;
alert(id+":"+lastName);
}
})
return false;
})
}); </script>
二、HttpMessageConverter
HttpMessageConverter 是 Spring3.0 新添加的一个接 口,负责将请求信息转换为一个对象(类型为 T),将对象( 类型为 T)输出为响应信息
使用 HttpMessageConverter 将请求信息转化并绑定到处理方法的入 参中或将响应结果转为对应类型的响应信息,Spring 提供了两种途径:
- 使用 @RequestBody / @ResponseBody 对处理方法进行标注
- 使用 HttpEntity / ResponseEntity 作为处理方法的入参或返回值
/**
* 测试使用ResponseEntity进行文件下载
* @param session
* @return
* @throws IOException
*/ @RequestMapping("/testResponseEntity")
public ResponseEntity<byte[]> testResponseEntity(HttpSession session) throws IOException {
byte [] body = null;
ServletContext servletContext = session.getServletContext();
InputStream in = servletContext.getResourceAsStream("/WEB-INF/pages/list.jsp");
body = new byte[in.available()];
in.read(body);
// 设置响应头
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Disposition", "attachment;filename=list.jsp");
//设置响应状态码
HttpStatus statusCode = HttpStatus.OK; ResponseEntity<byte[]> response = new ResponseEntity<byte[]>(body, headers, statusCode);
return response;
}
当控制器处理方法使用到 @RequestBody/@ResponseBody 或 HttpEntity/ResponseEntity 时, Spring 首先根据请求头或响应头的 Accept 属性选择匹配的 HttpMessageConverter, 进而根据参数类型或 泛型类型的过滤得到匹配的 HttpMessageConverter, 若找不到可用的 HttpMessageConverter 将报错 @RequestBody 和 @ResponseBody 不需要成对出现
三、源码分析
HttpMessageConverter 的实现类
- 调试进入org.springframework.web.servlet.DispatcherServlet.class
- 查看DispathcherServlet(this)的handlerAdapters
- 查看handlerAdapters下的 messageConverters
DispatcherServlet 默认装配 RequestMappingHandlerAdapter ,加入 jackson jar 包后, RequestMappingHandlerAdapter 装配的 HttpMessageConverter 如下:
org.springframework.http.converter.json.MappingJackson2HttpMessageConverter
SpringMVC——处理 JSON:使用 HttpMessageConverter的更多相关文章
- springMVC 处理json 及 HttpMessageConverter 接口
一.SpringMVC处理json的使用 1.添加依赖jar包 <dependency> <groupId>com.fasterxml.jackson.core</gro ...
- SpringMVC学习--json
简介 json数据格式在接口调用中.html页面中较常用,json格式比较简单,解析还比较方便.比如:webservice接口,传输json数据. springmvc与json交互 @RequestB ...
- SpringMVC关于json、xml自动转换的原理研究[附带源码分析 --转
SpringMVC关于json.xml自动转换的原理研究[附带源码分析] 原文地址:http://www.cnblogs.com/fangjian0423/p/springMVC-xml-json-c ...
- SpringMVC的JSON数据交互(七)-@Response,@RestController,@RequestBody用法
1.@RequestBody (自动将请求的数据封装为对象) 作用: @RequestBody注解用于读取http请求的内容(字符串),通过springmvc提供的HttpMessageConve ...
- springmvc实现json交互 -requestBody和responseBody
json数据交互 1.为什么要进行json数据交互 json数据格式在接口调用中.html页面中较常用,json格式比较简单,解析还比较方便. 比如:webservice接口,传输json数据. 2. ...
- 【Spring学习笔记-MVC-3】SpringMVC返回Json数据-方式1
<Spring学习笔记-MVC>系列文章,讲解返回json数据的文章共有3篇,分别为: [Spring学习笔记-MVC-3]SpringMVC返回Json数据-方式1:http://www ...
- springMVC源码分析--HttpMessageConverter写write操作(三)
上一篇博客springMVC源码分析--HttpMessageConverter参数read操作中我们已经简单介绍了参数值转换的read操作,接下来我们介绍一下返回值的处理操作.同样返回值的操作操作也 ...
- 【Spring学习笔记-MVC-3.1】SpringMVC返回Json数据-方式1-扩展
<Spring学习笔记-MVC>系列文章,讲解返回json数据的文章共有3篇,分别为: [Spring学习笔记-MVC-3]SpringMVC返回Json数据-方式1:http://www ...
- springMvc中406错误解决,springMvc使用json出现406 (Not Acceptable)
springMvc中406错误解决, springMvc使用json出现406 (Not Acceptable) >>>>>>>>>>> ...
随机推荐
- erlang 应用获取系统参数
很多时候,我们的程序需要一些预定义的参数,比如上次说的tcp_server的例子 一般参数有几种途径,具体参考这里http://blog.yufeng.info/archives/2852 app里面 ...
- Goclipse on Eclipse
Goclipse插件安装地址 https://goclipse.github.io/releases gocode.godef都可以通过Download快速安装 但guru容易失败:package g ...
- Difference between boot ip. service ip and persistent ip in hacmp
- boot IP is the original address on a network interface even when the cluster is down - service IP ...
- volley 发送post请求
public static void postNewComment(Context context,final UserAccount userAccount,final String comment ...
- MongoDB Data Model 浅谈
MongoDB 对于数据的 schema 要求很灵活. 与 MySQL 相比,collection 并不会强制文档的结构.(MySQL 在定义表时, 需要指定有哪些字段.类型.展示长度等) 因此,插入 ...
- 多线程设计模式(三):Master-Worker模式
Master-Worker模式是常用的并行模式之一,它的核心思想是,系统有两个进程协作工作:Master进程,负责接收和分配任务:Worker进程,负责处理子任务.当Worker进程将子任务处理完成后 ...
- logback高级特性二 异步记录日志
问题描述: 下图中JProfiler可看出logback的日志输出占了64%的cpu消耗 优化方案: 1. 这部分写日志的代码写了一些报文数据,确实是比较大的字符串.先禁掉控制台输出,生产环境也不需要 ...
- 1099 Build A Binary Search Tree
1099 Build A Binary Search Tree (30)(30 分) A Binary Search Tree (BST) is recursively defined as a bi ...
- 海外开发者推荐:10个顶级2D游戏资源站
转自:http://www.gamelook.com.cn/2015/12/239038 Gamelook报道/随着手游市场的持续增长,HTML5的发展以及大型发行商的支持,2D游戏的数量变得越来越多 ...
- C#一个判断子串在父串中出现的次数
/// <summary> /// 计算字符串中子串出现的次数 /// </summary> /// <param name=”str”>字符串</param ...