Srping3中配置

maven依赖pom.xml

需要jackson库的依赖

        <dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.13</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.13</version>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

SpringMVC配置

        <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJacksonHttpMessageConverter" />
</list>
</property>
</bean>
<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>apolication/json; charset=UTF-8</value>
</list>
</property>
</bean>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

接口

注意必须用@ResponseBody返回

    @RequestMapping(value = "/resource", method = RequestMethod.GET)
@ResponseBody
public List<Resource> getResource() { return resourceDao.findAll();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

Spring4配置

在Spring4中配置有所不同,否则返回错误

Caused by: java.lang.ClassNotFoundException: org.springframework.http.converter.json.MappingJacksonHttpMessageConverter
  • 1

maven依赖pom.xml

        <dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-hppc</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-joda</artifactId>
<version>${jackson.version}</version>
</dependency>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

SpringMVC配置

注意是MappingJackson2HttpMessageConverter,而不是MappingJacksonHttpMessageConverter

    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJacksonHttpMessageConverter" />
</list>
</property>
</bean>
<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>apolication/json; charset=UTF-8</value>
</list>
</property>
</bean>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14

接口

这里配置一样

    @RequestMapping(value = "/resource", method = RequestMethod.GET)
@ResponseBody
public List<Resource> getResource() { return resourceDao.findAll();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

测试

用firefox的RESTClient插件进行测试 

版权声明:本文为博主原创文章,未经博主允许不得转载。

怎样从SpringMVC返回json数据的更多相关文章

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

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

  2. 【Spring学习笔记-MVC-4】SpringMVC返回Json数据-方式2

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

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

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

  4. SpringMVC返回JSON数据时日期格式化问题

    https://dannywei.iteye.com/blog/2022929 SpringMVC返回JSON数据时日期格式化问题 博客分类: Spring   在运用SpringMVC框架开发时,可 ...

  5. springMVC返回json数据乱码问题及@RequestMapping 详解

    原文地址:https://blog.csdn.net/u010127245/article/details/51774074 一.@RequestMapping RequestMapping是一个用来 ...

  6. springMVC返回json数据乱码问

    在springMVC controller中返回json数据出现乱码问题,因为没有进行编码,只需要简单的注解就可以了 在@RequestMapping()中加入produces="text/ ...

  7. SpringMVC——返回JSON数据&&文件上传下载

    --------------------------------------------返回JSON数据------------------------------------------------ ...

  8. 关于springmvc 返回json数据null字段的显示问题-转https://blog.csdn.net/qq_23911069/article/details/62063450

    最近做项目(ssm框架)的时候,发现从后台返回的json(fastjson)数据对应不上实体类,从数据库查询的数据,如果对应的实体类的字段没有信息的话,json数据里面就不显示,这不是我想要的结果,准 ...

  9. springmvc 返回json数据给前台jsp页面展示

    spring mvc返回json字符串的方式 方案一:使用@ResponseBody 注解返回响应体 直接将返回值序列化json            优点:不需要自己再处理 步骤一:在spring- ...

  10. SpringMVC 返回JSON数据的配置

    spring-mvc-config.xml(文件名称请视具体情况而定)配置文件: <!-- 启动Springmvc注解驱动 --> <mvc:annotation-driven> ...

随机推荐

  1. gerrit使用总结

    1. 前言 最近在摸索git + gerrit代码审核的工作模式,需要安装gerrit服务器,经过一阵折腾,走了不少弯路,在此做下笔记,以便自己或者他人参考.有误的地方请指教. 我的环境如下: 环境 ...

  2. ChartCtrl源码剖析之——CChartLegend类

    CChartLegend类用来绘制每一个波形的描述信息,它处于该控件的区域,如下图所示: CChartLegend类的头文件. #if !defined(AFX_CHARTLEGEND_H__CD72 ...

  3. javascript 中==与===

    1.==相等运算符 1.1如果操作数具有相同的类型,则判断其等同性,如果两个数的值相等,则返回true(相等),否则返回 false (不相等) 1.2如果两个操作数类型不同,则按如下规则: null ...

  4. 值得网页设计师&前端收藏的实用工具列表

    原文地址:http://www.uisdc.com/tool-list-web-developers# 无论你是经验丰富的前端,还是刚刚起步的设计师,这些为真正的网页设计师和开发者所准备的实用工具.在 ...

  5. Gym 100531B Buffcraft (贪心+暴力+前缀和)

    题意:给定两个加血的方式,一个是直接加多少,另一种是加百分之几,然后你能够你选 k 种,问你选哪 k 种. 析:首先肯定要选加的多的,所以我们先排序,从大到小,然后用前缀和存储一下,再去枚举从第一种和 ...

  6. 你不知道的Eclipse用法:全局搜索和更替 (转载)

    转自:http://blog.csdn.net/p106786860/article/details/9162537 Eclipse中全局搜索和更替 Eclipse全局搜索步骤 使用快捷键“ctrl+ ...

  7. centos 6.4 源码安装php5.4 mysql5.5 apahce2

    centos 6.4 源码安装php5.4 mysql5.5 apahce2 博客分类: php   参考:http://blog.csdn.net/simpleiseasy/article/deta ...

  8. Java-String 类的常用方法

    Java 中 String 类的常用方法 Ⅰ String 类提供了许多用来处理字符串的方法,例如,获取字符串长度.对字符串进行截取.将字符串转换为大写或小写.字符串分割等,下面我们就来领略它的强大之 ...

  9. mysql创建数据库在硬盘位置 for Mac

    使用HomeBrew安装为/usr/local/var/mysql 使用官方下载的dmg镜像安装为/usr/local/mysql

  10. codeforces 570 D. Tree Requests (dfs)

    题目链接: 570 D. Tree Requests 题目描述: 给出一棵树,有n个节点,1号节点为根节点深度为1.每个节点都有一个字母代替,问以结点x为根的子树中高度为h的后代是否能够经过从新排序变 ...