1. 程式人生 > >springmvc實現restful返回xml格式的字串

springmvc實現restful返回xml格式的字串

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

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

在本示例中,我們將向您展示如何將物件轉換成xml格式並通過spring mvc框架返回給使用者。

技術及環境:

  1. Spring 3.0.5.RELEASE
  2. JDK 1.6
  3. Eclipse 3.6
  4. Maven 3

1、新增專案依賴

不需要更多,你只要新增spring mvc的依賴即可:
  1. <properties>
  2. <spring.version>3.0.5.RELEASE</spring.version>
  3. </properties>
  4. <dependencies>
  5. <!-- Spring 3 dependencies -->
  6. <dependency>
  7. <groupId>org.springframework</groupId>
  8. <artifactId>spring-core</artifactId>
  9. <version>${spring.version}</version>
  10. </dependency>
  11. <dependency>
  12. <groupId>org.springframework</groupId>
  13. <artifactId>spring-web</artifactId>
  14. <version>${spring.version}</version>
  15. </dependency>
  16. <dependency>
  17. <groupId>org.springframework</groupId>
  18. <artifactId>spring-webmvc</artifactId>
  19. <version>${spring.version}</version>
  20. </dependency>
  21. </dependencies>

2、實體類JavaBean

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

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

  1. import javax.xml.bind.annotation.XmlElement;
  2. import javax.xml.bind.annotation.XmlRootElement;
  3. @XmlRootElement(name ="coffee")
  4. publicclassCoffee{
  5. String name;
  6. int quanlity;
  7. publicString getName(){
  8. return name;
  9. }
  10. @XmlElement
  11. publicvoid setName(String name){
  12. this.name = name;
  13. }
  14. publicint getQuanlity(){
  15. return quanlity;
  16. }
  17. @XmlElement
  18. publicvoid setQuanlity(int quanlity){
  19. this.quanlity = quanlity;
  20. }
  21. publicCoffee(String name,int quanlity){
  22. this.name = name;
  23. this.quanlity = quanlity;
  24. }
  25. publicCoffee(){
  26. }
  27. }

3、Controller

新增@ResponseBody註解到你的方法返回值,在spring文件中沒有太多的細節,它會自動處理轉換。

  1. import org.springframework.stereotype.Controller;
  2. import org.springframework.web.bind.annotation.PathVariable;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. import org.springframework.web.bind.annotation.RequestMethod;
  5. import org.springframework.web.bind.annotation.ResponseBody;
  6. import com.mkyong.common.model.Coffee;
  7. @Controller
  8. @RequestMapping("/coffee")
  9. publicclassXMLController{
  10. @RequestMapping(value="{name}", method =RequestMethod.GET)
  11. public@ResponseBodyCoffee getCoffeeInXML(@PathVariableString name){
  12. Coffee coffee =newCoffee(name,100);
  13. return coffee;
  14. }
  15. }

4、mvc:annotation-driven

在你的spring配置檔案中,啟用mvc:annotation-driven註解。

  1. <beansxmlns="http://www.springframework.org/schema/beans"
  2. xmlns:context="http://www.springframework.org/schema/context"
  3. xmlns:mvc="http://www.springframework.org/schema/mvc"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="
  6. http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context-3.0.xsd
  10. http://www.springframework.org/schema/mvc
  11. http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
  12. <context:component-scanbase-package="com.mkyong.common.controller"/>
  13. <mvc:annotation-driven/>
  14. </beans>

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

  1. <beans ...>
  2. <beanclass="org.springframework.web.servlet.view.BeanNameViewResolver"/>
  3. <beanid="xmlViewer"
  4. class="org.springframework.web.servlet.view.xml.MarshallingView">
  5. <constructor-arg>
  6. <beanclass="org.springframework.oxm.jaxb.Jaxb2Marshaller">
  7. <propertyname="classesToBeBound">
  8. <list>
  9. <value>com.mkyong.common.model.Coffee</value>
  10. </list>
  11. </property>
  12. </bean>
  13. </constructor-arg>
  14. </bean>
  15. </beans>

5、示例結果

spring-mvc-xml-demo

相關推薦

springmvc實現restful返回xml格式字串

最近,想在自己的小專案中搭建一個Restful風格的服務介面api,專案用的spring mvc 3,聽說spring mvc本身就能十分方便的支援restful的實現,於是查詢了下資料,果然非常強大。 在一次偶然的#牆#外#(你懂的)狀態下瀏覽到了一個老外的部落格,舉

Spring MVC 實現RESTful 返回JSON格式資料

在Java後端的日常開發中,系統間呼叫通過使用json格式資料,本文將向你展示如何將物件轉換成json格式並通過Spring MVC框架返回給呼叫者。 開發工具配置 Spring 4.2.7.RELEASE Jackson 2.6.7 JDK 1.7

Spring Boot rest api 返回 XML 格式的資料

Spring Boot 預設返回json 格式的資料,Rest Api 可以根據使用者請求頭的不同 ,返回不同的媒體型別的響應(JSON XML 等)在預設的情況下,Spring 會安裝應用所定義的內容協商策略解析正確的內容 (使用者可以根據指定 Accept 頭資訊來返回不同型別的資訊

SpringMVC內容協商返回jsonxml格式

springMVC內容協商需要引入以下包 <!--springMVC內容協商需要引入以下包--> <dependency> <groupId>com.fasterxml.jackson.dataformat</gr

springMVC實現restful風格

/** * 使用springMVC實現restful跳轉頁面 * @return */ @RequestMapping(value="toPage/{page}",method = RequestMethod.GET) public String

使用SpringMVC 實現RESTful,並解決PUT,DELETE請求無法提交表單資料的問題

瞭解RESTful,使用SpringMVC 實現RESTful 關於REST: 1.表述性狀態轉移,是web服務的一種架構風格,是一種思想,而非標準或軟體。 2. 通常基於使用HTTP,URI,XML、JSON、HTML這些現廣泛流行的協議。 3.屬於輕量級(使

Spring Boot rest api 返回 XML 格式的資料

Spring Boot 預設返回json 格式的資料,Rest Api 可以根據使用者請求頭的不同 ,返回不同的媒體型別的響應(JSON XML 等)在預設的情況下,Spring 會安裝應用所定義的內容協商策略解析正確的內容 (使用者可以根據指定 Accept

REST、RESTFUL的理解以及SpringMVC實現Restful程式設計

其實HTTP 1.1協議的整體軟體架構就可以說是REST架構 瞭解REST得知道5個名字: 1、資源 Resource 資源就是伺服器上獲取到的東西都可以說是資源,一條使用者記錄,一個使用者的密碼,一張圖片等等都是 2、資源的表述 Representation

SpringMVC實現RESTful

RESTful:表現層狀態轉換。是一種設計風格。可以使url更簡潔,使系統更安全。   RESTful: 1. 需要在傳輸資料時以   /${引數名} 的格式,不是以往的 ?XX=XX 格式傳輸 <a href="/del/${job.id}

hapi返回xml格式 微信開發 node

圈內,使用Koa2、express比較多,而我hapi使用比較多。目前在做微信公眾號開發,要求返回資料是xml格式。 1、之前的返回,直接return Json2Xml: async functio

php介面返回xml格式

 在頭部加上 header("Content-type: application/xml");  例子: $res = $this->arrayToXml($data,$amount['count'],$page_count,$page); header(

Qt讀寫xml格式字串

Xml 簡介 W3CXML教程 XML是ExtensibleMarkup Language的縮寫,即可擴充套件標記語言。它是一種用來建立的標記的標記語言。使用XML標記語言可以做到資料或資料結構在任何程式語言環境下的共享。 XML 被設計用來傳輸和

通過JDK的JAXBContext類來轉換java實體類bean和xml格式字串的一些問題和解決辦法

    這兩天工作中用到web service來推送資料,涉及到xml的和java實體類的轉換,總結一下理解和碰到的問題。    (一)、寶寶想得到下面這樣的兩個xml型別的字串anth和args,來推送給第三方:<?xml version="1.0" encoding

.NET新建webservice專案並返回XML格式資料

1.開啟VS2013,新建一個專案,如圖; 2.進入專案介面,新增新的專案,如圖; 出現如下介面就新建專案完成啦! 3.執行,如圖; 點選Hello World;

AJAX返回XML格式文字的讀取方法

對於一個AJAX請求如果返回的是標準的XML(有<?xml version="1.0" encoding="UTF-8"?>,並且ContentType = "text/xml"),則直接操作xmlhttp.responseXML應該是可以的,比如:var req

WebService 返回json格式返回xml格式的資料

返回json格式 //using System.Web.Script.Services; [WebMethod] [ScriptMethod(UseHttpGet =

使用webservice返回xml格式資料使用jq解析

<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>

dom4j解析xml檔案 或者xml格式字串

  主要記錄的是專案中遇到的解析xml報文 獲取指定資料的方法,怕以後會忘,記錄一下   導包:dom4j(版本根據需要,不會造成相容錯誤就可以)  主要思路:利用遞迴的形式,解析xml檔案,首先附錄遞迴的方法,因為要求是需要把抓取到的資料放到指定檔案,並且現在只是一個de

springMVC框架下返回json格式的物件,list,map

注意這個例子要使用jquery,但是jquery檔案屬於靜態的資原始檔,所以要在springMVC中設定靜態資源訪問 web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app xml

使用dom4j解析xml格式字串,獲取標籤屬性和內容

本文解析XML格式字串引入的是: dom4j-1.6.1.jar具體程式碼如下: /**      * 解析xml字串,獲取各項屬性內容      * readXML方法描述:      *      * @author : sunyan      * @createTi