麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > JSP > 正文

Spring mvc實現Restful返回xml格式數據實例詳解

2024-09-05 00:22:55
字體:
來源:轉載
供稿:網友

spring mvc實現Restful返回xml格式數據

最近,想在自己的小項目中搭建一個Restful風格的服務接口api,項目用的spring mvc 3,聽說spring mvc本身就能十分方便的支持restful的實現,于是查詢了下資料,果然非常強大。

在一次偶然的#墻#外#(你懂的)狀態下瀏覽到了一個老外的博客,舉了幾個入門例子十分經典,原文是E文+被墻狀態,覺得有必要扒過來收藏學習下。

在本示例中,我們將向您展示如何將對象轉換成xml格式并通過spring mvc框架返回給用戶。

技術及環境:

Spring 3.0.5.RELEASE
JDK 1.6
Eclipse 3.6
Maven 3

1、添加項目依賴

不需要更多,你只要添加spring mvc的依賴即可:

<properties> <spring.version>3.0.5.RELEASE</spring.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></dependencies>

2、實體類JavaBean

一個簡單的JavaBean,添加了JAXB 注解,稍后將會被轉換成xml。

JAXB已經包含在JDK1.6中,你不需要添加額外的依賴庫,只需要使用注解,spring會自動將其轉換為xml格式。

import javax.xml.bind.annotation.XmlElement;import javax.xml.bind.annotation.XmlRootElement;@XmlRootElement(name = "coffee")public class Coffee { String name; int quanlity; public String getName() { return name; } @XmlElement public void setName(String name) { this.name = name; } public int getQuanlity() { return quanlity; } @XmlElement public void setQuanlity(int quanlity) { this.quanlity = quanlity; } public Coffee(String name, int quanlity) { this.name = name; this.quanlity = quanlity; } public Coffee() { }}

3、Controller

添加@ResponseBody注解到你的方法返回值,在spring文檔中沒有太多的細節,它會自動處理轉換。

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.Coffee;@Controller@RequestMapping("/coffee")public class XMLController { @RequestMapping(value="{name}", method = RequestMethod.GET) public @ResponseBody Coffee getCoffeeInXML(@PathVariable String name) { Coffee coffee = new Coffee(name, 100); return coffee; }}

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>

或者,你也可以添加spring-oxm.jar依賴,并用以下的MarshallingView處理轉換,使用這種方法,你可以不用在方法中使用@ResponseBody注解。

<beans ...> <bean class="org.springframework.web.servlet.view.BeanNameViewResolver" /> <bean id="xmlViewer"  class="org.springframework.web.servlet.view.xml.MarshallingView"> <constructor-arg> <bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller"> <property name="classesToBeBound"> <list>  <value>com.mkyong.common.model.Coffee</value> </list> </property> </bean> </constructor-arg> </bean></beans>

5、示例結果

訪問URL:http://localhost:8080/SpringMVC/rest/coffee/arabica

spring,mvc,Restful返回xml格式數據

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!


注:相關教程知識閱讀請移步到JSP教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 国产1区2 | 深夜福利久久久 | 久久久激情网 | 亚洲aⅴ免费在线观看 | 国产精品一区二区三区在线看 | 91久久久久久久一区二区 | 久久精品在线免费观看 | 国产羞羞视频 | av不卡毛片| 国产精品视频yy9299一区 | 在火车上摸两乳爽的大叫 | 中文字幕精品在线观看 | 久久人人av | 在线97视频| 性高湖久久久久久久久aaaaa | 9999久久久久久 | 黄色成人在线 | 人人玩人人爽 | 中文字幕网站在线 | 亚洲精品动漫在线观看 | 制服丝袜成人动漫 | 九九精品视频观看 | 蜜桃成品人免费视频 | 一级在线视频 | 久久久久久久黄色片 | 成年免费在线视频 | 毛片a级毛片免费播放100 | 久久久一区二区三区视频 | 毛片在线免费观看完整版 | 国产精品成人久久 | 强伦女教师视频 | 久久吊| 在线高清中文字幕 | 亚洲va久久久噜噜噜久牛牛影视 | 成人性视频免费网站下载软件 | 日韩精品中文字幕一区二区三区 | 国产精品视频成人 | 羞羞网站视频 | 1314av| 国产精品自拍片 | av电影院在线观看 |