Spring 3 MVC and JSON example
2015-09-14 17:22
375 查看
In this tutorial, we show you how to output JSON data in Spring MVC framework.
Technologies used :
Spring 3.2.2.RELEASE
Jackson 1.9.10
JDK 1.6
Eclipse 3.6
Maven 3
P.S In Spring 3, to output JSON data, just puts Jackson library in the project classpath.
Jackson library is existed in the project classpath
The
Return method annotated with
Spring will handle the JSON conversion automatically.
4.
Enable
Technologies used :
Spring 3.2.2.RELEASE
Jackson 1.9.10
JDK 1.6
Eclipse 3.6
Maven 3
P.S In Spring 3, to output JSON data, just puts Jackson library in the project classpath.
1. Project Dependencies
Get Jackson and Spring dependencies.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> < 4000 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. Model
A simple POJO, later output this object as formatted JSON data.package com.mkyong.common.model; public class Shop { String name; String staffName[]; //getter and setter methods }
3. Controller
Add@ResponseBodyas return value. Wen Spring sees
Jackson library is existed in the project classpath
The
mvc:annotation-drivenis enabled
Return method annotated with
@ResponseBody
Spring will handle the JSON conversion automatically.
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
Enable mvc:annotation-drivenin your Spring configuration XML file.
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. Demo
URL : http://localhost:8080/SpringMVC/rest/kfc/brands/kfc-kampar相关文章推荐
- 分享微信开发Html5轻游戏中的几个坑
- 一个jar包里的网站
- 一个jar包里的网站之文件上传
- 一个jar包里的网站之返回对媒体类型
- MyEclipse Web Project转Eclipse Dynamic Web Project
- Windows Clang开发环境备忘
- Ubuntu 安装 JDK 问题
- XML 与 JSON 优劣对比
- VBA将excel数据表生成JSON文件
- newtonsoft.json解析天气数据出错解决方法
- vbs 解析json jsonp的方法
- Extjs4如何处理后台json数据中日期和时间
- C#实现将类的内容写成JSON格式字符串的方法
- JQuery ajax返回JSON时的处理方式 (三种方式)
- 傻瓜式用Eclipse连接MySQL数据库
- jquery JSON的解析方式示例介绍
- c#版json数据解析示例分享
- ASP JSON类文件的使用方法
- C#实现对Json字符串处理实例
- C#实现Json转Unicode的方法