一,引入fasterxm.jackson包

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.6.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.6.3</version>
</dependency>
二,配置spring-servlet.xml

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">

<property name="messageConverters">

<list>

<ref bean="mappingJackson2HttpMessageConverter" />

</list>

</property>

</bean>

<bean id="mappingJackson2HttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">

<property name="supportedMediaTypes">

<list>

<value>text/html;charset=UTF-8</value>

<value>text/json;charset=UTF-8</value>

<value>application/json;charset=UTF-8</value>

</list>

</property>

</bean>

三,后台controller类,使用@ReponseBody关键字可以将结果(一个包含字符串和JavaBean的Map),转换成JSON

@Controller

@RequestMapping("/user")

public class DemoController {

@RequestMapping(value = "/list", method = RequestMethod.GET)

@ResponseBody

public Map<String, Object> getUserList() {

logger.info("列表");

List<UserModel> list = new ArrayList<UserModel>();

UserModel um = new UserModel();

um.setId("1");

um.setUsername("sss");

um.setAge(222);

list.add(um);

Map<String, Object> modelMap = new HashMap<String, Object>(3);

modelMap.put("total", "1");

modelMap.put("data", list);

modelMap.put("success", "true");

return modelMap;

}

}

直接在地址栏请求:

springmvc 用fasterxml.jackson返回son数据的更多相关文章

  1. ajax返回son数据

    JSON 只是一种文本字符串.它被存储在 responseText 属性中 为了读取存储在 responseText 属性中的 JSON 数据,需要根据 JavaScript 的 eval 语句. 函 ...

  2. springMVC返回json数据时date类型数据被转成long类型

    在项目的过程中肯定会遇到ajax请求,但是再用的过程中会发现,在数据库中好好的时间类型数据:2017-05-04 17:52:24 在转json的时候,得到的就不是时间格式了 而是145245121这 ...

  3. springmvc配置接口返回的数据是json

    首先要导入所需要的jar,使用maven方式管理jar包 <!-- 配置接口返回数据json --> <dependency> <groupId>com.faste ...

  4. 练习:将值是null的数据删除掉(剔除):com.fasterxml.jackson.annotation.JsonInclude;包

    练习:将值是null的数据删除掉(剔除):com.fasterxml.jackson.annotation.JsonInclude;包 例如,有数据是null,不想展示 { "statusC ...

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

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

  6. 怎样从SpringMVC返回json数据

    Srping3中配置 maven依赖pom.xml 需要jackson库的依赖 <dependency> <groupId>org.codehaus.jackson</g ...

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

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

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

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

  9. SpringMVC 配置.html拦截时,返回JSON数据时出现406错误解决方案

    [说明]在SpringMVC框架的使用中常常会使用@ResponseBody注解,修饰"处理器"(Controller的方法),这样在处理器在返回完毕后,就不走逻辑视图,而是将返回 ...

随机推荐

  1. c#读取MySQL数据表中的内容

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  2. JavaEE JDBC 了解JNDI

    了解JNDI @author ixenos Web与企业应用中的连接管理 1. 数据库连接方式: (1)使用配置文件 (2)使用JNDI 2. 在Web或企业环境中部署 JDBC应用时,数据库连接管理 ...

  3. hdu 3038带权并查集

    #include<stdio.h> #include<string.h> #define N  200100 struct node { int x,count; }pre[N ...

  4. [NOIP2006] 提高组 洛谷P1066 2^k进制数

    题目描述 设r是个2^k 进制数,并满足以下条件: (1)r至少是个2位的2^k 进制数. (2)作为2^k 进制数,除最后一位外,r的每一位严格小于它右边相邻的那一位. (3)将r转换为2进制数q后 ...

  5. 一练Splay之维修数列第一次

    平衡树并不是之前没写过,觉得有必要把平衡树变成考场上能敲的东西,也就是说,考一道诸如“维修数列”这样的送分题,要能拿满分. 维修数列.给定一个数列支持以下操作: 输入的第1 行包含两个数N 和M(M ...

  6. csu - 1536: Bit String Reordering (模拟)

    http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1536 不知道为何怎么写都写不对. 这题可以模拟. 虽然题目保证一定可以从原串变成目标串,但是不一定 ...

  7. Test for Job 图上的动态规划(DAG)

    Test for Job Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 11399   Accepted: 2697 Des ...

  8. I - Balancing Act POJ - 1655

    Consider a tree T with N (1 <= N <= 20,000) nodes numbered 1...N. Deleting any node from the t ...

  9. 51Nod——T 1686 第K大区间

    https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1686 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 ...

  10. I/O---BufferedInputStream及相关类介绍

    关于BufferedInputStream 是java提供的具有缓存作用的字节输入流.与之对应的还有BufferedOutStream 和 BufferedRead 和BufferedWriter 这 ...