一、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的更多相关文章

  1. springMVC 处理json 及 HttpMessageConverter 接口

    一.SpringMVC处理json的使用 1.添加依赖jar包 <dependency> <groupId>com.fasterxml.jackson.core</gro ...

  2. SpringMVC学习--json

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

  3. SpringMVC关于json、xml自动转换的原理研究[附带源码分析 --转

    SpringMVC关于json.xml自动转换的原理研究[附带源码分析] 原文地址:http://www.cnblogs.com/fangjian0423/p/springMVC-xml-json-c ...

  4. SpringMVC的JSON数据交互(七)-@Response,@RestController,@RequestBody用法

    1.@RequestBody   (自动将请求的数据封装为对象) 作用: @RequestBody注解用于读取http请求的内容(字符串),通过springmvc提供的HttpMessageConve ...

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

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

  6. 【Spring学习笔记-MVC-3】SpringMVC返回Json数据-方式1

    <Spring学习笔记-MVC>系列文章,讲解返回json数据的文章共有3篇,分别为: [Spring学习笔记-MVC-3]SpringMVC返回Json数据-方式1:http://www ...

  7. springMVC源码分析--HttpMessageConverter写write操作(三)

    上一篇博客springMVC源码分析--HttpMessageConverter参数read操作中我们已经简单介绍了参数值转换的read操作,接下来我们介绍一下返回值的处理操作.同样返回值的操作操作也 ...

  8. 【Spring学习笔记-MVC-3.1】SpringMVC返回Json数据-方式1-扩展

    <Spring学习笔记-MVC>系列文章,讲解返回json数据的文章共有3篇,分别为: [Spring学习笔记-MVC-3]SpringMVC返回Json数据-方式1:http://www ...

  9. springMvc中406错误解决,springMvc使用json出现406 (Not Acceptable)

    springMvc中406错误解决, springMvc使用json出现406 (Not Acceptable) >>>>>>>>>>> ...

随机推荐

  1. 【转载】对一致性Hash算法,Java代码实现的深入研究

    原文地址:http://www.cnblogs.com/xrq730/p/5186728.html 一致性Hash算法 关于一致性Hash算法,在我之前的博文中已经有多次提到了,MemCache超详细 ...

  2. 十一、python沉淀之路--map函数、filter函数、reduce函数、匿名函数、内置函数

    一.map函数 1.自定义函数,实现类似于map函数的功能 num_l = [1,3,4,5,6,9] def power(n): return n ** 2 def map_test(func,ar ...

  3. SMMS 2016 啟用深色主題

    1.用文本類編輯器 打開C:\Program Files (x86)\Microsoft SQL Server\130\Tools\Binn\ManagementStudio目錄下的 ssms.pkg ...

  4. log框架集成

    使用slf4j,slf4j相当于一个接口,我们面对接口编程,方便地集成其他的日志框架,我们按照slf4j的标准,日志就会相应地打入日志系统中(log4j 使用slf4j要有两个包1,他本身的api,2 ...

  5. COGS 2259 异化多肽——生成函数+多项式求逆

    题目:http://cogs.pro:8080/cogs/problem/problem.php?pid=2259 详见:https://www.cnblogs.com/Zinn/p/10054569 ...

  6. Python学习之变量的作用域

    学习地址:http://www.jianshu.com/p/17a9d8584530 1.变量作用域LEGB 1.1变量的作用域 在Python程序中创建.改变.查找变量名时,都是在一个保存变量名的空 ...

  7. vue参考

    https://github.com/taylorchen709/vue-admin http://element-cn.eleme.io/#/zh-CN/component/layout https ...

  8. 让Eclipse的TomcatPlugin支持Tomcat 8.x

     使用tomcat插件启动项目的优势: 1.TomcatPlugin是一个免重启的开发插件,原始的Servers方式启动tomcat项目,修改xxx.ftl  或者 xxx.jsp 文件后需要重启to ...

  9. 别人的dubbo学习笔记

    本文转载自:http://blog.csdn.net/tao_qq/article/details/49952229 学习dubbo,开始做一些笔记. 1> 启动dubbo-admin模块的时候 ...

  10. 使用CMake生成sln项目和VS工程遇到的问题

    用vs运行cmake后的工程 参考:http://zhidao.baidu.com/link?url=AZRxI0jGDzo6Pikk68qylee0g7leXbpbZGiVuyiijWbd8scUK ...