国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html
内部邀请码:C8E245J (不写邀请码,没有现金送)
国内私募机构九鼎控股打造,九鼎投资是在全国股份转让系统挂牌的公众公司,股票代码为430719,为“中国PE第一股”,市值超1000亿元。 
------------------------------------------------------------------------------------------------------------------------------------------------------------------

老外写的文章真的很易懂!   
原文地址:http://www.mkyong.com/spring-mvc/spring-3-mvc-and-json-example/

Published: July 28, 2011 , Updated: July 27, 2011 , Author: mkyong
 
In Spring 3, you can enable “mvc:annotation-driven” to support object conversion to/from JSON format, if Jackson JSON processor is existed on the project classpath.

In this tutorial, we show you how to output JSON data from Spring MVC.

Technologies used :

Spring 3.0.5.RELEASE
Jackson 1.7.1
JDK 1.6
Eclipse 3.6
Maven 3

1. Project Dependencies

To use JSON in Spring MVC, you need to include Jackson dependency.

<properties>
<spring.version>3.0.5.RELEASE</spring.version>
</properties> <dependencies> <!-- Jackson JSON Mapper -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.9.9</version>
</dependency> <!-- Spring 3 dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency> <dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency> </dependencies>

2. Model

A simple POJO, later convert this object into JSON output.

package com.mkyong.common.model;

public class Shop {

    String name;
String staffName[]; //getter and setter methods }

3. Controller

Add “@ResponseBody” in the return value, no much detail in the Spring documentation.

As i know, when Spring see Jackson library existed on classpath, “mvc:annotation-driven” is enabled. Return method annotated with @ResponseBody.It will handle the JSON conversion automatically.

package com.mkyong.common.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.mkyong.common.model.Shop; @Controller
@RequestMapping("/kfc/brands")
public class JSONController { @RequestMapping(value="{name}", method = RequestMethod.GET)
public @ResponseBody Shop getShopInJSON(@PathVariable String name) { Shop shop = new Shop();
shop.setName(name);
shop.setStaffName(new String[]{"mkyong1", "mkyong2"}); return shop; } }

4. mvc:annotation-driven

Enable “mvc:annotation-driven” in your Spring configuration XML file.

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> <context:component-scan base-package="com.mkyong.common.controller" /> <mvc:annotation-driven /> </beans>

5. Demo

URL : http://localhost:8080/SpringMVC/rest/kfc/brands/kfc-kampar

Download Source Code

Download it – SpringMVC-Json-Example.zip (21 KB)

References

  1. mvc-annotation-driven documentation
  2. High-performance JSON processor
  3. Spring MVC and XML example

Spring 3 MVC and JSON example的更多相关文章

  1. spring 3 mvc hello world + mavern +jetty

    Spring 3 MVC hello world example By mkyong | August 2, 2011 | Updated : June 15, 2015 In this tutori ...

  2. spring mvc返回json字符串的方式

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

  3. spring mvc返回json字符串数据,只需要返回一个java bean对象就行,只要这个java bean 对象实现了序列化serializeable

    1.spring mvc返回json数据,只需要返回一个java bean对象就行,只要这个java bean 对象实现了序列化serializeable 2. @RequestMapping(val ...

  4. Spring Mvc 输出Json(iwantmoon.com出品)

    原文:http://iwantmoon.com/Post/f94e49caf9b6455db7158474bab4c4dd 因为工作需要,现在要去做开放平台,考虑了多种方案之后,基本确定 下来,Htt ...

  5. Spring MVC 的json问题(406 Not Acceptable)

    原因 : 就是程序转换JSON失败. 在pom.xml 加上 <dependency> <groupId>com.fasterxml.jackson.core</grou ...

  6. spring mvc接收JSON格式的参数

    1.配置spring解析json的库   <dependency>         <groupId>org.codehaus.jackson</groupId> ...

  7. ajax使用向Spring MVC发送JSON数据出现 org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported错误

    ajax使用向Spring MVC发送JSON数据时,后端Controller在接受JSON数据时报org.springframework.web.HttpMediaTypeNotSupportedE ...

  8. Spring MVC生成JSON数据

    以下示例演示如何使用Spring Web MVC框架生成JSON数据格式.首先使用Eclipse IDE,并按照以下步骤使用Spring Web Framework开发基于动态表单的Web应用程序: ...

  9. Spring Boot——2分钟构建spring web mvc REST风格HelloWorld

    之前有一篇<5分钟构建spring web mvc REST风格HelloWorld>介绍了普通方式开发spring web mvc web service.接下来看看使用spring b ...

随机推荐

  1. CentOS7.1 Xshell 经常掉线 Connection closed by foreign host

    XShell如果经常对CentOS掉线,则VNC肯定连接不上 但是ping CentOS的IP又能ping通,主要原因还是因为sshd的设置问题 #进入ssh目录 cd /etc/ssh #修改ssh ...

  2. Oracle EBS PO 收接事处理状态待定或错误

    PO接收后,发现在没有接收成功.在"事务处理状态汇总"查找到不是"待定"就是"错误",如下图:   对于事务处理状态"待定&quo ...

  3. Ubuntu 12.04下搭建Qt开发环境

    http://download.qt.io/official_releases/qt/ Ubuntu 环境下Gtk与Qt编译环境安装与配置(系统环境是Ubuntu 12.04) 1.配置基础开发环境G ...

  4. linux 上下文切换带来的影响

    1.保存CPU寄存器中的内容 2.CPU高速缓存中的内容失效 3.重新装载页表,用于给线程程安装一个新的虚拟地址空间,页表缓存失效

  5. 记一次SSH登陆失败问题的定位

    创建用户之后,使用ssh协议登陆提示失败. useradd -d /home/hdp -m hdp -g dba -s /bin/bash 通过命令 ssh -v hdp@127.0.0.1 登陆,查 ...

  6. 8.2/baltic神(水)题

    summary:10 bzoj1334: Description N个政党要组成一个联合内阁,每个党都有自己的席位数. 现在希望你找出一种方案,你选中的党的席位数要大于总数的一半,并且联合内阁的席位数 ...

  7. unix network programming(3rd)Vol.1 [第13~15章]《读书笔记系列》

    第13章 守护进程和inetd 超级服务器 syslog() daemon_init() setuid() setgid() 第14章 高级IO 标准I/O函数库,支持3种缓冲 缓冲(读写存储设备(硬 ...

  8. devexpress 中Grid 的使用:为零不显示

    如果要让为0的列不显示: this.gridColumn_FAmount.DisplayFormat.FormatType = DevExpress.Utils.FormatType.Numeric; ...

  9. OpenGL 加载位图(BMP)贴图

    GLuint Texture[1]; HBITMAP hBMP; BITMAP BMP; glGenTextures(1, &Texture[0]); hBMP = (HBITMAP)Load ...

  10. MVC框架模式技术实例(用到隐藏帧、json、仿Ajax、Dom4j、jstl、el等)

    前言: 刚刚学完了MVC,根据自己的感悟和理解写了一个小项目. 完全按照MVC模式,后面有一个MVC的理解示意图. 用MVC模式重新完成了联系人的管理系统: 用户需求: 多用户系统,提供用户注册.登录 ...