婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁(yè) > 知識(shí)庫(kù) > Spring mvc實(shí)現(xiàn)Restful返回xml格式數(shù)據(jù)實(shí)例詳解

Spring mvc實(shí)現(xiàn)Restful返回xml格式數(shù)據(jù)實(shí)例詳解

熱門標(biāo)簽:地圖標(biāo)注市場(chǎng)怎么樣 如何用中國(guó)地圖標(biāo)注數(shù)字點(diǎn) 企業(yè)怎么在聯(lián)通申請(qǐng)400電話 百度地圖添加標(biāo)注圖標(biāo)樣式 南昌市地圖標(biāo)注app 泰州泰興400電話 怎么申請(qǐng) 聊城智能電銷機(jī)器人外呼 南京新思維電話機(jī)器人 好操作的電話機(jī)器人廠家

spring mvc實(shí)現(xiàn)Restful返回xml格式數(shù)據(jù)

最近,想在自己的小項(xiàng)目中搭建一個(gè)Restful風(fēng)格的服務(wù)接口api,項(xiàng)目用的spring mvc 3,聽說spring mvc本身就能十分方便的支持restful的實(shí)現(xiàn),于是查詢了下資料,果然非常強(qiáng)大。

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

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

技術(shù)及環(huán)境:

Spring 3.0.5.RELEASE
JDK 1.6
Eclipse 3.6
Maven 3

1、添加項(xiàng)目依賴

不需要更多,你只要添加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、實(shí)體類JavaBean

一個(gè)簡(jiǎn)單的JavaBean,添加了JAXB 注解,稍后將會(huì)被轉(zhuǎn)換成xml。

JAXB已經(jīng)包含在JDK1.6中,你不需要添加額外的依賴庫(kù),只需要使用注解,spring會(huì)自動(dòng)將其轉(zhuǎn)換為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文檔中沒有太多的細(xì)節(jié),它會(huì)自動(dòng)處理轉(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.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處理轉(zhuǎn)換,使用這種方法,你可以不用在方法中使用@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、示例結(jié)果

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

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

您可能感興趣的文章:
  • Spring mvc JSON數(shù)據(jù)交換格式原理解析
  • Java SpringMVC框架開發(fā)之?dāng)?shù)據(jù)導(dǎo)出Excel文件格式實(shí)例詳解
  • springMVC返回復(fù)雜的json格式數(shù)據(jù)方法
  • Spring MVC通過添加自定義注解格式化數(shù)據(jù)的方法
  • Spring mvc實(shí)現(xiàn)Restful返回json格式數(shù)據(jù)實(shí)例詳解
  • SpringMVC中Json數(shù)據(jù)格式轉(zhuǎn)換
  • 解決SpringMVC 返回Java8 時(shí)間JSON數(shù)據(jù)的格式化問題處理
  • SpringMVC環(huán)境下實(shí)現(xiàn)的Ajax異步請(qǐng)求JSON格式數(shù)據(jù)
  • Springmvc數(shù)據(jù)格式化原理及代碼案例

標(biāo)簽:開封 吉林 銅川 山南 自貢 白銀 烏蘭察布 臨汾

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Spring mvc實(shí)現(xiàn)Restful返回xml格式數(shù)據(jù)實(shí)例詳解》,本文關(guān)鍵詞  Spring,mvc,實(shí)現(xiàn),Restful,返回,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Spring mvc實(shí)現(xiàn)Restful返回xml格式數(shù)據(jù)實(shí)例詳解》相關(guān)的同類信息!
  • 本頁(yè)收集關(guān)于Spring mvc實(shí)現(xiàn)Restful返回xml格式數(shù)據(jù)實(shí)例詳解的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 南充市| 林西县| 济南市| 邯郸县| 江山市| 江门市| 登封市| 肇州县| 揭阳市| 城市| 乾安县| 那坡县| 象州县| 临泉县| 醴陵市| 景德镇市| 会泽县| 平顶山市| 沅江市| 习水县| 泸州市| 滨海县| 甘孜县| 瑞昌市| 庐江县| 加查县| 布拖县| 滦南县| 新巴尔虎左旗| 逊克县| 阳春市| 武安市| 桃江县| 库车县| 楚雄市| 邵阳市| 肥乡县| 平遥县| 灵石县| 微山县| 丹棱县|