Spring3 MVC 整合JSON 实例

原文链接:http://www.mkyong.com/spring-mvc/spring-3-mvc-and-json-example/

之前看到的一篇文章,关于Spring MVC 怎么整合JSON,并返回JSON格式的内容,这篇文章讲得很简明清晰,所以把他稍微翻译一下,转载于此。

====================================================================================================================================

 

这个教程,将会向你展示如何在Spring MVC框架中返回JSON格式内容。

使用到的技术:

  1. Spring 3.2.2.RELEASE
  2. Jackson 1.9.10
  3. JDK 1.6
  4. Eclipse 3.6
  5. Maven 3

PS:在Spring 3 版本,只需要将JackSon包放在classpath根目录下就行。

1.项目依赖配置

(注:这部分是相对于Maven项目来说的,如果不是Maven管理的项目,可以不理会这部分,直接把相关Sring包和Jackson包导入lib库即可。)

配置Jackson 和Spring依赖

pom.xml如下

<project xmlns="http://maven.apache.org/POM/4.0.0" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 
        http://maven.apache.org/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.mkyong.common</groupId>
	<artifactId>SpringMVC</artifactId>
	<packaging>war</packaging>
	<version>1.0-SNAPSHOT</version>
	<name>SpringMVC Json Webapp</name>
	<url>http://maven.apache.org</url>
 
	<properties>
		<spring.version>3.2.2.RELEASE</spring.version>
		<jackson.version>1.9.10</jackson.version>
		<jdk.version>1.6</jdk.version>
	</properties>
 
	<dependencies>
 
		<!-- 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>
 
		<!-- Jackson JSON Mapper -->
		<dependency>
			<groupId>org.codehaus.jackson</groupId>
			<artifactId>jackson-mapper-asl</artifactId>
			<version>${jackson.version}</version>
		</dependency>
 
	</dependencies>
 
	<build>
		<finalName>SpringMVC</finalName>
		<plugins>
		  <plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-eclipse-plugin</artifactId>
			<version>2.9</version>
			<configuration>
				<downloadSources>true</downloadSources>
				<downloadJavadocs>false</downloadJavadocs>
				<wtpversion>2.0</wtpversion>
			</configuration>
		  </plugin>
		  <plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-compiler-plugin</artifactId>
			<version>2.3.2</version>
			<configuration>
				<source>${jdk.version}</source>
				<target>${jdk.version}</target>
			</configuration>
		  </plugin>
		</plugins>
	</build>
 
</project>

2.模型

一个简单的pojo对象,稍后将会被转化为json格式数据输出。

package com.mkyong.common.model;
 
public class Shop {
 
	String name;
	String staffName[];
 
	//getter and setter methods
 
}

 

3.控制器

在方法名前面增加@ResponseBody注解。如果Spring发现

  1. 项目lib库中含有Jackson包
  2. Spring启用了 mvc:annotation-driven MVC注解配置
  3. 返回的方法使用了@ResponseBody注解

Spring 会自动进行JSON的转换。

JSONController.java如下所示:

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 注解

mvc-dispatcher-servlet.xml

<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. 样例

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

http://www.mkyong.com/wp-content/uploads/2011/07/spring-mvc-json-demo.png

 

样例源码下载:

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

 

参考:

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

 

你可能感兴趣的:(spring,json)