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

  1. SpringMVC——处理 JSON:使用 HttpMessageConverter

    一.SpringMVC处理JSON流程 1. 加入 jar 包: jackson-annotations-2.1.5.jarjackson-core-2.1.5.jarjackson-databind ...

  2. SpringMVC学习--json

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

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

    目录 前言 现象 源码分析 实例讲解 关于配置 总结 参考资料 前言 SpringMVC是目前主流的Web MVC框架之一. 如果有同学对它不熟悉,那么请参考它的入门blog:http://www.c ...

  4. SpringMVC关于json、xml自动转换的原理研究

    SpringMVC是目前主流的Web MVC框架之一. 如果有同学对它不熟悉,那么请参考它的入门blog:http://www.cnblogs.com/fangjian0423/p/springMVC ...

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

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

  6. SpringMVC中Json数据格式转换

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

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

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

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

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

  9. SpringMVC关于json、xml自动转换的原理研究[附带源码分析](使用JAXB转换XML)

    前言 SpringMVC是目前主流的Web MVC框架之一. 如果有同学对它不熟悉,那么请参考它的入门blog:http://www.cnblogs.com/fangjian0423/p/spring ...

随机推荐

  1. [leetcode]2. Add Two Numbers.cpp

    You are given two non-empty linked lists representing two non-negative integers. The digits are stor ...

  2. CentOS(十二)--crontab命令的使用方法

    crontab命令常见于Unix和Linux的操作系统之中,用于设置周期性被执行的指令.该命令从标准输入设备读取指令,并将其存放于"crontab"文件中,以供之后读取和执行. 在 ...

  3. layer.js 中弹框显示不全的问题

    在使用 layer.js 做弹框的时候,遇到在浏览器缩小时,弹框显示不全的问题,如下: 这是不行的,因为我们有的时候想缩小浏览器视窗,但是一旦缩小到一定程度,就会把弹窗的关闭按钮遮住一部分,并且主体弹 ...

  4. 安装Caffe纪实

    第一章 引言 在ubuntu16.04安装caffe,几乎折腾了一个月终于成功;做一文章做纪要,以便日后查阅.总体得出的要点是:首先,每操作一步,必须知道如何检验操作的正确性;笔者的多次失误是因为配置 ...

  5. 通过DMS连接RDS需要添加的DMS白名单地址

    10.152.163.0/24,139.224.4.0/24,11.193.54.0/24,101.37.74.0/24,10.137.42.0/24,121.43.18.0/24

  6. JavaScript:BOM&DOM

    BOM(Browser Object Model): feature: 1.BOM has no relevant standards. 2.The fundamental object of BOM ...

  7. Python中的logging模块【转】https://www.cnblogs.com/yelin/p/6600325.html

    [转]https://www.cnblogs.com/yelin/p/6600325.html 基本用法 下面的代码展示了logging最基本的用法. 1 # -*- coding: utf-8 -* ...

  8. mallo

    http://luleimi.blog.163.com/blog/static/175219645201302295042968/

  9. selenium下打开Chrome报错解决

    错误如下: [22516:20196:0704/024642.979:ERROR:install_util.cc(597)] Unable to read registry value HKLM\SO ...

  10. Selenium Java关闭浏览器

    在学习selenium的过程中发现一个问题,各种博客/教程都是教人用selenium的quit()和close()方法关闭浏览器. 但这不是我要的结果.这两个方法的前提是,用webdriver打开浏览 ...