Spring mvc實(shí)現(xiàn)Restful返回json格式數(shù)據(jù)實(shí)例詳解
在本示例中,我們將向您展示如何將對(duì)象轉(zhuǎn)換成json格式并通過(guò)spring mvc框架返回給用戶。
使用技術(shù)及環(huán)境:
- Spring 3.2.2.RELEASE
- Jackson 1.9.10
- JDK 1.6
- Eclipse 3.6
- Maven 3
PS:在spring 3 中,要輸出json數(shù)據(jù),只需要添加Jackson 庫(kù)到你的classpath。
1、項(xiàng)目依賴
spring和jackson的依賴:
<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、Model
一個(gè)簡(jiǎn)單的JavaBean,稍后將被轉(zhuǎn)換成json格式輸出。
public class Shop { String name; String staffName[]; //getter and setter methods }
3、Controller
添加@ResponseBody到返回值,我們看到:
Jackson 包已經(jīng)在項(xiàng)目的 classpath
mvc:annotation-driven注解已經(jīng)啟用
返回方法已經(jīng)添加了@ResponseBody
spring會(huì)自動(dòng)處理json的轉(zhuǎn)換。
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](https://my.oschina.net/u/1774615) @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、示例結(jié)果
訪問(wèn)URL:http://localhost:8080/SpringMVC/rest/kfc/brands/kfc-kampar
spring-mvc-json-demo
感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!
- Spring mvc JSON數(shù)據(jù)交換格式原理解析
- Java SpringMVC框架開(kāi)發(fā)之?dāng)?shù)據(jù)導(dǎo)出Excel文件格式實(shí)例詳解
- springMVC返回復(fù)雜的json格式數(shù)據(jù)方法
- Spring MVC通過(guò)添加自定義注解格式化數(shù)據(jù)的方法
- Spring mvc實(shí)現(xiàn)Restful返回xml格式數(shù)據(jù)實(shí)例詳解
- SpringMVC中Json數(shù)據(jù)格式轉(zhuǎn)換
- 解決SpringMVC 返回Java8 時(shí)間JSON數(shù)據(jù)的格式化問(wèn)題處理
- SpringMVC環(huán)境下實(shí)現(xiàn)的Ajax異步請(qǐng)求JSON格式數(shù)據(jù)
- Springmvc數(shù)據(jù)格式化原理及代碼案例
相關(guān)文章
JSP開(kāi)發(fā)之Spring方法注入之替換方法實(shí)現(xiàn)
這篇文章主要介紹了JSP開(kāi)發(fā)之Spring方法注入之替換方法實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2017-07-07jsp實(shí)現(xiàn)生成中國(guó)國(guó)旗圖片效果代碼
這篇文章主要介紹了jsp實(shí)現(xiàn)生成中國(guó)國(guó)旗圖片效果代碼,涉及JSP圖形繪制及數(shù)學(xué)運(yùn)算的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10Java動(dòng)態(tài)代理實(shí)現(xiàn)AOP
Java動(dòng)態(tài)代理實(shí)現(xiàn)AOP...2006-10-10jsp簡(jiǎn)單連接SQL Server2000數(shù)據(jù)庫(kù)的方法
這篇文章主要介紹了jsp簡(jiǎn)單連接SQL Server2000數(shù)據(jù)庫(kù)的方法,較為詳細(xì)的分析了JSP連接SQL Server2000數(shù)據(jù)庫(kù)的相關(guān)設(shè)置技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10J2ME/J2EE實(shí)現(xiàn)用戶登錄交互 實(shí)現(xiàn)代碼
用手機(jī)客戶端進(jìn)行登錄服務(wù)器,然后返回消息進(jìn)行交互.2009-07-07JAVA/JSP學(xué)習(xí)系列之二(Tomcat安裝)
JAVA/JSP學(xué)習(xí)系列之二(Tomcat安裝)...2006-10-10JSP技術(shù)實(shí)現(xiàn)RSS訂閱功能的示例
用JSP技術(shù)實(shí)現(xiàn)RSS功能是如何做到的呢?本文將向你介紹一下具體的實(shí)現(xiàn),如果你對(duì)用JSP技術(shù)實(shí)現(xiàn)RSS功能感興趣,那么這篇文章是你的選擇。2016-10-10