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. public class Coffee {
  5. String name;
  6. int quanlity;
  7. public String getName() {
  8. return name;
  9. }
  10. @XmlElement
  11. public void setName(String name) {
  12. this.name = name;
  13. }
  14. public int getQuanlity() {
  15. return quanlity;
  16. }
  17. @XmlElement
  18. public void setQuanlity(int quanlity) {
  19. this.quanlity = quanlity;
  20. }
  21. public Coffee(String name, int quanlity) {
  22. this.name = name;
  23. this.quanlity = quanlity;
  24. }
  25. public Coffee() {
  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. public class XMLController {
  10. @RequestMapping(value="{name}", method = RequestMethod.GET)
  11. public @ResponseBody Coffee getCoffeeInXML(@PathVariable String name) {
  12. Coffee coffee = new Coffee(name, 100);
  13. return coffee;
  14. }
  15. }

4、mvc:annotation-driven

在你的spring配置文件中,啓用mvc:annotation-driven註解。

  1. <beans xmlns="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-scan base-package="com.mkyong.common.controller" />
  13. <mvc:annotation-driven />
  14. </beans>

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

  1. <beans ...>
  2. <bean class="org.springframework.web.servlet.view.BeanNameViewResolver" />
  3. <bean id="xmlViewer"
  4. class="org.springframework.web.servlet.view.xml.MarshallingView">
  5. <constructor-arg>
  6. <bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
  7. <property name="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、示例結果

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

spring-mvc-xml-demo

原文鏈接:http://www.mkyong.com/spring-mvc/spring-3-mvc-and-json-example/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章