Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2534608
  • 博文数量: 245
  • 博客积分: 4125
  • 博客等级: 上校
  • 技术积分: 3113
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-25 23:56
文章分类

全部博文(245)

文章存档

2015年(2)

2014年(26)

2013年(41)

2012年(40)

2011年(134)

2010年(2)

分类: Java

2011-10-31 11:10:16

在spring 3中,如果 (json处理器)已经存在于项目的类路径,你可以打开 “” 来支持对象与json的转换。

在这个教程里,主要介绍怎样从spring mvc中输出json数据。

主要用到的技术如下:


  1. Spring 3.0.5.RELEASE
  2. Jackson 1.7.1
  3. JDK 1.6
  4. Eclipse 3.6
  5. Maven 3

1. 项目依赖

为了在spring mvc中使用json,你需要包含Jackson依赖项。

  1. <properties>
  2.         <spring.version>3.0.5.RELEASE</spring.version>
  3.     </properties>
  4.  
  5.     <dependencies>
  6.  
  7.         <!-- Jackson JSON Mapper -->
  8.         <dependency>
  9.             <groupId>org.codehaus.jackson</groupId>
  10.             <artifactId>jackson-mapper-asl</artifactId>
  11.             <version>1.7.1</version>
  12.         </dependency>
  13.  
  14.         <!-- Spring 3 dependencies -->
  15.         <dependency>
  16.             <groupId>org.springframework</groupId>
  17.             <artifactId>spring-core</artifactId>
  18.             <version>${spring.version}</version>
  19.         </dependency>
  20.  
  21.         <dependency>
  22.             <groupId>org.springframework</groupId>
  23.             <artifactId>spring-web</artifactId>
  24.             <version>${spring.version}</version>
  25.         </dependency>
  26.  
  27.         <dependency>
  28.             <groupId>org.springframework</groupId>
  29.             <artifactId>spring-webmvc</artifactId>
  30.             <version>${spring.version}</version>
  31.         </dependency>
  32.  
  33.     </dependencies>

2. 模型Model

一个简单的POJO, 接下来我们将把这个对象转换为json格式。

  1. package com.mkyong.common.model;
  2.  
  3. public class Shop {
  4.  
  5.     String name;
  6.     String staffName[];
  7.  
  8.     //getter and setter methods
  9.  
  10. }

3.控制器 Controller

在控制器中方法的返回值前面加上 “@ResponseBody” , 在 Spring documentation中介绍比较少

据我了解,当spring发现

  1. Jackson 类库存在于类路径中
  2. “mvc:annotation-driven” 已经打开
  3. Return method annotated with @ResponseBody

它就会自动的处理json转换。

  1. package com.mkyong.common.controller;
  2.  
  3. import org.springframework.stereotype.Controller;
  4. import org.springframework.web.bind.annotation.PathVariable;
  5. import org.springframework.web.bind.annotation.RequestMapping;
  6. import org.springframework.web.bind.annotation.RequestMethod;
  7. import org.springframework.web.bind.annotation.ResponseBody;
  8. import com.mkyong.common.model.Shop;
  9.  
  10. @Controller
  11. @RequestMapping("/kfc/brands")
  12. public class JSONController {
  13.  
  14.     @RequestMapping(value="{name}", method = RequestMethod.GET)
  15.     public @ResponseBody Shop getShopInJSON(@PathVariable String name) {
  16.  
  17.         Shop shop = new Shop();
  18.         shop.setName(name);
  19.         shop.setStaffName(new String[]{"mkyong1", "mkyong2"});
  20.  
  21.         return shop;
  22.  
  23.     }
  24.  
  25. }


4. mvc:annotation-driven

在你的spring配置文件中打开“mvc:annotation-driven” 。

  1. <beans xmlns=""
  2.     xmlns:context=""
  3.     xmlns:mvc=""
  4.     xmlns:xsi=""
  5.     xsi:schemaLocation="
  6.         
  7.         /spring-beans-3.0.xsd
  8.         
  9.         /spring-context-3.0.xsd
  10.         
  11.         /spring-mvc-3.0.xsd">
  12.  
  13.     <context:component-scan base-package="com.mkyong.common.controller" />
  14.  
  15.     <mvc:annotation-driven />
  16.  
  17. </beans>

5. Demo

URL : 









谢谢访问!

阅读(31161) | 评论(1) | 转发(0) |
给主人留下些什么吧!~~

最代码2014-04-28 18:00:04

请参考代码:SpringMVC整合JSON、XML视图,下载地址:http://www.zuidaima.com/share/1751862737554432.htm