spring json
引用:http://blog.chinaunix.net/uid-21209537-id-2983728.html
在spring 3中,如果Jackson (json处理器)已经存在于项目的类路径,你可以打开 “mvc:annotation-driven”来支持对象与json的转换。
在这个教程里,主要介绍怎样从spring mvc中输出json数据。
主要用到的技术如下:
- Spring 3.0.5.RELEASE
- Jackson 1.7.1
- JDK 1.6
- Eclipse 3.6
- Maven 3
1. 项目依赖
为了在spring mvc中使用json,你需要包含Jackson依赖项。
- <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.7.1</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
一个简单的POJO, 接下来我们将把这个对象转换为json格式。
- package com.mkyong.common.model;
- public class Shop {
- String name;
- String staffName[];
- //getter and setter methods
- }
3.控制器 Controller
在控制器中方法的返回值前面加上 “@ResponseBody” , 在 Spring documentation中介绍比较少
据我了解,当spring发现
- Jackson 类库存在于类路径中
- “mvc:annotation-driven” 已经打开
- Return method annotated with @ResponseBody
它就会自动的处理json转换。
- 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
在你的spring配置文件中打开“mvc:annotation-driven” 。
- <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