spring json

引用:http://blog.chinaunix.net/uid-21209537-id-2983728.html

在spring 3中,如果Jackson (json处理器)已经存在于项目的类路径,你可以打开 “mvc:annotation-driven”来支持对象与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>
  34.  

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="http://www.springframework.org/schema/beans"
  2.     xmlns:context="http://www.springframework.org/schema/context"
  3.     xmlns:mvc="http://www.springframework.org/schema/mvc" 
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5.     xsi:schemaLocation="
  6.         http://www.springframework.org/schema/beans 
  7.         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  8.         http://www.springframework.org/schema/context 
  9.         http://www.springframework.org/schema/context/spring-context-3.0.xsd
  10.         http://www.springframework.org/schema/mvc
  11.         http://www.springframework.org/schema/mvc/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 : http://localhost:8080/SpringMVC/rest/kfc/brands/kfc-kampar




原文地址:https://www.cnblogs.com/sode/p/2778756.html