原創不易,轉載請註明出處:SpringMVC整合JSON、XML視圖html
代碼下載地址:http://www.zuidaima.com/share/1751862737554432.htmjava
SpringMVC中整合了JSON、XML的視圖,能夠經過這些視圖完成Java對象 到XML、JSON的轉換。轉換XML提供了MarshallingView,開發者只需用注入相應的marshaller、和屬性配置,便可自動完成 Java的Model對象中的數據到XML的編組。web
Email:hoojo_@126.comspring
Blog:http://blog.csdn.net/IBM_hoojo數組
1、 準備工做spring-mvc
一、 本次程序會涉及到Jackson、xStream、Jibx、Jaxb二、castor等技術,若是你對這些技術還不是很瞭解。建議閱讀:http://www.cnblogs.com/hoojo/archive/2011/04/27/2030264.htmlmvc
這篇文章中涉及到的內容應該對你有很多幫助。app
二、 jar包下載jsp
spring各版本jar下載地址:http://ebr.springsource.com/repository/app/library/detail?name=org.springframework.spring
相關的依賴包也能夠在這裏找到:http://ebr.springsource.com/repository/app/library
三、 至少須要如下jar包
四、 當前工程的web.xml配置
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <-- 配置Spring核心控制器 --> <servlet> <servlet-name>dispatcher</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/dispatcher.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dispatcher</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <-- 解決工程編碼過濾器 --> <filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
五、 WEB-INF中的dispatcher.xml配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" 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/mvc http://www.springframework.org/schema/mvc/spring-mvc-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/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"> <-- 註解探測器 --> <context:component-scan base-package="com.hoo.controller"/> <-- 視圖解析器,根據視圖的名稱new ModelAndView(name),在配置文件查找對應的bean配置 --> <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"> <property name="order" value="1"/> </bean> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> </bean> </beans>
啓動後,能夠看到index.jsp 沒有出現異常或錯誤。那麼當前SpringMVC的配置就成功了。
2、 利用Jaxb2編組XML
一、 Jaxb2能夠完成XML和Java的相互轉換,在WebService中用得較多。前面也介紹過Jaxb2 的用法。
在線博文:
For cnblogs:http://www.cnblogs.com/hoojo/archive/2011/04/26/2029011.html
For csdn:http://blog.csdn.net/IBM_hoojo/archive/2011/04/26/6363491.aspx
二、 首先在dispatcher.xml中配置Jaxb2的marshaller的視圖,配置以下:
<bean name="jaxb2MarshallingView" class="org.springframework.web.servlet.view.xml.MarshallingView"> <constructor-arg> <bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller"> <property name="classesToBeBound"> <array> <value>com.hoo.entity.User</value> <value>com.hoo.entity.AccountBean</value> <value>com.hoo.entity.MapBean</value> <value>com.hoo.entity.ListBean</value> </array> </property> </bean> </constructor-arg> </bean>
Jaxb2的jar在jdk中已經包含,因此不須要添加額外的jar包。詳細信息你能夠參考1中的博文。上面的 jaxb2MarshallingView視圖的class是MarshallingView,它有一個構造器須要傳遞一個Marshaller。 Marshaller主要完成編組,即將Java對象轉換成XML的這麼一個東東。咱們在這個構造器中注入了Jaxb2Marshaller這個類,這個 bean有一個classesToBeBound屬性,這個屬性是一個數組。須要將即將轉換成XML的Java對象配置在這裏。並且這些對象須要進行 Annotation註解。
三、 建立Jaxb2MarshallingViewController,完成Java對象到XML的轉換
單個JavaBean的轉換,代碼以下:
package com.hoo.controller; import java.util.ArrayList; import java.util.Date; import java.util.List; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import com.hoo.entity.AccountBean; import com.hoo.entity.Brithday; import com.hoo.entity.DifferBean; import com.hoo.entity.ListBean; import com.hoo.entity.MoreBean; import com.hoo.entity.User; /** * <b>function:</b>Jaxb2MarshallingView 視圖,利用Jaxb2進行Java對象到XML的轉換技術 * @author hoojo * @createDate 2011-4-27 下午03:20:23 * @file Jaxb2MarshallingViewController.java * @package com.hoo.controller * @project SpringMVC4View * @blog http://blog.csdn.net/IBM_hoojo * @email hoojo_@126.com * @version 1.0 */ @Controller @RequestMapping("/jaxb2/view") public class Jaxb2MarshallingViewController { /* * MarshallingView Jaxb2Marshaller 須要配置轉換成xml的java對象的Annotation */ @RequestMapping("/doXMLJaxb2") public ModelAndView doXMLJaxb2View() { System.out.println("#################ViewController doXMLJaxb2View##################"); ModelAndView mav = new ModelAndView("jaxb2MarshallingView"); AccountBean bean = new AccountBean(); bean.setAddress("address"); bean.setEmail("email"); bean.setId(1); bean.setName("haha"); Brithday day = new Brithday(); day.setBrithday("2010-11-22"); bean.setBrithday(day); mav.addObject(bean); return mav; } }
上面的代碼的ModelAndView配置了jaxb2MarshallingView這個視圖,就表示結果集會經過這個視圖進行編組後顯示。上面須要轉換的AccountBean和Birthday對象,這些對象須要配置annotation,前面已經講到annotation對JavaBean轉換XML的做用。咱們來看看AccountBean對象代碼:
package com.hoo.entity; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = "account") public class AccountBean { private int id; private String name; private String email; private String address; private Brithday brithday; @XmlElement public Brithday getBrithday() { return brithday; } public void setBrithday(Brithday brithday) { this.brithday = brithday; } @XmlElement public int getId() { return id; } public void setId(int id) { this.id = id; } @XmlElement public String getName() { return name; } public void setName(String name) { this.name = name; } @XmlElement public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } @XmlElement public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return this.name + "#" + this.id + "#" + this.address + "#" + this.brithday + "#" + this.email; } }
在getter方法都有部分註解,若是你想了解更多的jaxb2的相關技術,參考:http://www.cnblogs.com/hoojo/archive/2011/04/26/2029011.html
Brithday
package com.hoo.entity; public class Brithday { private String brithday; public Brithday() {} public Brithday(String brithday) { this.brithday = brithday; } public String getBrithday() { return brithday; } public void setBrithday(String brithday) { this.brithday = brithday; } }
Brithday是AccountBean中的一個屬性,在AccountBean中已經註解過。這裏就不須要進行註解配置。
經過瀏覽器請求:http://localhost:8080/SpringMVC4View/jaxb2/view/doXMLJaxb2.do
結果以下:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <account><address>address</address><brithday><brithday>2010-11-22</brithday></brithday> <email>email</email><id>1</id><name>haha</name></account>
四、 轉換帶List屬性的JavaEntity
/** * <b>function:</b>轉換帶有List屬性的JavaBean * @author hoojo * @createDate 2011-4-27 下午05:32:22 * @return */ @RequestMapping("/doListXMLJaxb2") public ModelAndView doListXMLJaxb2View() { System.out.println("#################ViewController doListXMLJaxb2View##################"); ModelAndView mav = new ModelAndView("jaxb2MarshallingView"); List<Object> beans = new ArrayList<Object>(); for (int i = 0; i < 3; i++) { AccountBean bean = new AccountBean(); bean.setAddress("address#" + i); bean.setEmail("email" + i + "@12" + i + ".com"); bean.setId(1 + i); bean.setName("haha#" + i); Brithday day = new Brithday(); day.setBrithday("2010-11-2" + i); bean.setBrithday(day); beans.add(bean); User user = new User(); user.setAddress("china GuangZhou# " + i); user.setAge(23 + i); user.setBrithday(new Date()); user.setName("jack#" + i); user.setSex(Boolean.parseBoolean(i + "")); beans.add(user); } ListBean list = new ListBean(); list.setList(beans); mav.addObject(list); return mav; }
ListBean註解過的代碼
package com.hoo.entity; import java.util.List; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlElements; import javax.xml.bind.annotation.XmlRootElement; @SuppressWarnings("unchecked") @XmlRootElement public class ListBean { private String name; private List list; @XmlElements({ @XmlElement(name = "user", type = User.class), @XmlElement(name = "account", type = AccountBean.class), }) public List getList() { return list; } public void setList(List list) { this.list = list; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
經過上面的註解能夠看出List中只能存儲User、AccountBean對象,關於User對象的代碼和AccountBean對象的是同樣的,只是類名不一樣而已。讀者能夠本身添加。在WebBrowser中請求:http://localhost:8080/SpringMVC4View/jaxb2/view/doListXMLJaxb2.do
結果以下:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <listBean> <account><address>address#0</address><brithday><brithday>2010-11-20</brithday></brithday> <email>email0@120.com</email><id>1</id><name>haha#0</name></account> <user><address>china GuangZhou# 0</address><age>23</age> <brithday>2011-04-27T17:02:38.028+08:00</brithday><name>jack#0</name><sex>false</sex></user> <account><address>address#1</address><brithday><brithday>2010-11-21</brithday></brithday> <email>email1@121.com</email><id>2</id><name>haha#1</name></account> <user><address>china GuangZhou# 1</address><age>24</age> <brithday>2011-04-27T17:02:38.028+08:00</brithday><name>jack#1</name><sex>false</sex></user> <account><address>address#2</address><brithday><brithday>2010-11-22</brithday></brithday> <email>email2@122.com</email><id>3</id><name>haha#2</name></account> <user><address>china GuangZhou# 2</address><age>25</age> <brithday>2011-04-27T17:02:38.028+08:00</brithday><name>jack#2</name><sex>false</sex></user> </listBean>
五、 轉換帶有Map屬性的JavaBean,Jaxb2轉換Map有點複雜,先看看代碼:
/** * <b>function:</b>轉換帶有Map屬性的JavaBean * @author hoojo * @createDate 2011-4-27 下午05:32:42 * @return */ @RequestMapping("/doMapXMLJaxb2") public ModelAndView doMapXMLJaxb2View() { System.out.println("#################ViewController doMapXMLJaxb2View##################"); ModelAndView mav = new ModelAndView("jaxb2MarshallingView"); MapBean mapBean = new MapBean(); HashMap<String, AccountBean> map = new HashMap<String, AccountBean>(); AccountBean bean = new AccountBean(); bean.setAddress("北京"); bean.setEmail("email"); bean.setId(1); bean.setName("jack"); Brithday day = new Brithday(); day.setBrithday("2010-11-22"); bean.setBrithday(day); map.put("NO1", bean); bean = new AccountBean(); bean.setAddress("china"); bean.setEmail("tom@125.com"); bean.setId(2); bean.setName("tom"); day = new Brithday("2011-11-22"); bean.setBrithday(day); map.put("NO2", bean); mapBean.setMap(map); mav.addObject(mapBean); return mav; }
首先看看MapBean的代碼,代碼很簡單就一個Map的屬性。
package com.hoo.entity; import java.util.HashMap; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; import com.hoo.util.MapAdapter; @XmlRootElement public class MapBean { private HashMap<String, AccountBean> map; @XmlJavaTypeAdapter(MapAdapter.class) public HashMap<String, AccountBean> getMap() { return map; } public void setMap(HashMap<String, AccountBean> map) { this.map = map; } }
上面的代碼的getMap方法設置了XmlJavaTypeAdapter這個註解,註解裏面的MapAdapter是咱們本身實現的,並且還要構建一個MapElements數組元素。主要是繼承XmlAdapter重寫裏面的幾個方法完成的。
MapAdapter代碼
package com.hoo.util; import java.util.HashMap; import java.util.Map; import javax.xml.bind.annotation.adapters.XmlAdapter; import com.hoo.entity.AccountBean; /** * <b>function:</b>AccountBean 編組、解組的XmlAdapter * @author hoojo * @createDate 2011-4-25 下午05:03:18 * @file MyAdetper.java * @package com.hoo.util * @project WebHttpUtils * @blog http://blog.csdn.net/IBM_hoojo * @email hoojo_@126.com * @version 1.0 */ public class MapAdapter extends XmlAdapter<MapElements[], Map<String, AccountBean>> { public MapElements[] marshal(Map<String, AccountBean> arg0) throws Exception { MapElements[] mapElements = new MapElements[arg0.size()]; int i = 0; for (Map.Entry<String, AccountBean> entry : arg0.entrySet()) mapElements[i++] = new MapElements(entry.getKey(), entry.getValue()); return mapElements; } public Map<String, AccountBean> unmarshal(MapElements[] arg0) throws Exception { Map<String, AccountBean> r = new HashMap<String, AccountBean>(); for (MapElements mapelement : arg0) r.put(mapelement.key, mapelement.value); return r; } }
MapElements代碼
package com.hoo.util; import javax.xml.bind.annotation.XmlElement; import com.hoo.entity.AccountBean; /** * <b>function:</b> MapElements * @author hoojo * @createDate 2011-4-25 下午05:04:04 * @file MyElements.java * @package com.hoo.util * @project WebHttpUtils * @blog http://blog.csdn.net/IBM_hoojo * @email hoojo_@126.com * @version 1.0 */ public class MapElements { @XmlElement public String key; @XmlElement public AccountBean value; @SuppressWarnings("unused") private MapElements() { } // Required by JAXB public MapElements(String key, AccountBean value) { this.key = key; this.value = value; } }
在瀏覽器中請求:http://localhost:8080/SpringMVC4View/jaxb2/view/doMapXMLJaxb2.do
結果以下:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <mapBean><map><item><key>NO2</key><value><address>china</address> <brithday><brithday>2011-11-22</brithday></brithday> <email>tom@125.com</email><id>2</id><name>tom</name></value></item> <item><key>NO1</key><value><address>北京</address><brithday><brithday>2010-11-22</brithday></brithday> <email>email</email><id>1</id><name>jack</name></value></item></map> </mapBean>
總結,若是你想將一些Java的基本類型轉換成XML。那麼你得建立一個帶getter、setter方法的JavaBean。而後在Bean的getter方法進行相應的Annotation註解便可完成轉換。
3、 利用xStream轉換XML
一、 xStream能夠輕易的將Java對象轉換成XML、JSON,Spring整合利用xStream轉換xml。須要添加xStream相關的xstream-1.3.1.jar包。 若是你對xStream不上很瞭解,你能夠先閱讀這篇文章:
For csblogs:http://www.cnblogs.com/hoojo/archive/2011/04/22/2025197.html
For csdn:http://blog.csdn.net/IBM_hoojo/archive/2011/04/22/6342386.aspx
二、 而後在dispatcher.xml中添加以下配置
<-- xml視圖,XStreamMarshaller,能夠轉換任何形式的java對象,須要添加xStream jar包 --> <bean name="xStreamMarshallingView" class="org.springframework.web.servlet.view.xml.MarshallingView"> <property name="marshaller"> <bean class="org.springframework.oxm.xstream.XStreamMarshaller"> <-- 啓用annotation --> <property name="autodetectAnnotations" value="true"/> <-- 類名別名 --> <property name="aliases"> <map> <-- Account這個類的別名就變成了myBeans,那麼轉換後的xml中就是myBeans --> <entry key="myBeans" value="com.hoo.entity.Account"/> </map> </property> <-- 基本屬性別名 --> <property name="fieldAliases"> <map> <-- Account中的brithday這個屬性 --> <entry key="com.hoo.entity.Account.brithday" value="生日"/> </map> </property> </bean> </property> </bean>
上次配置的參數有註釋描述,還要沒有配置的參數。如:annotatedClass、annotatedClasses是當沒有配置啓用 annotation的時候,能夠用這2個屬性進行配置你指定的class啓用annotation註解。streamDriver是配置驅動用的,默認 能夠不要驅動,你能夠配置DomDriver、JSON相關的驅動。encoding是設置編碼,關於XStreamMarshaller還要更多的參數 配置和上面xStream 的博文中講解的同樣使用,只是經過配置,而博文中是直接在代碼中寫的。固然也能夠經過annotation進行註解哦;若是你想了解更多xStream的 用法,請你仔細閱讀:http://www.cnblogs.com/hoojo/archive/2011/04/22/2025197.html
三、 普通JavaBean轉換XML
package com.hoo.controller; import java.util.ArrayList; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import com.hoo.entity.Account; import com.hoo.entity.AccountArray; import com.hoo.entity.AccountBean; import com.hoo.entity.Brithday; import com.hoo.entity.DifferBean; import com.hoo.entity.ListBean; import com.hoo.entity.MapBean; import com.hoo.entity.MoreBean; import com.hoo.entity.User; /** * <b>function:</b>Jaxb2MarshallingView 視圖,利用Jaxb2進行Java對象到XML的轉換技術 * @author hoojo * @createDate 2011-4-27 下午03:20:23 * @file Jaxb2MarshallingViewController.java * @package com.hoo.controller * @project SpringMVC4View * @blog http://blog.csdn.net/IBM_hoojo * @email hoojo_@126.com * @version 1.0 */ @Controller @RequestMapping("/jaxb2/view") public class Jaxb2MarshallingViewController { /* * MarshallingView Jaxb2Marshaller 須要配置轉換成xml的java對象的Annotation */ @RequestMapping("/doXMLJaxb2") public ModelAndView doXMLJaxb2View() { System.out.println("#################ViewController doXMLJaxb2View##################"); ModelAndView mav = new ModelAndView("jaxb2MarshallingView"); AccountBean bean = new AccountBean(); bean.setAddress("address"); bean.setEmail("email"); bean.setId(1); bean.setName("haha"); Brithday day = new Brithday(); day.setBrithday("2010-11-22"); bean.setBrithday(day); mav.addObject(bean); return mav; } }
AccountBean上面的代碼已經給出,這裏就不重複了。值得說明的是xStream在ModelAndView中,直接用addObject 方法添加時,有時候出現一些不是咱們轉換的對象的信息,通常是BindingResult的xml信息。解決辦法就是設置addObject的key。 Key設置爲BindingResult.MODEL_KEY_PREFIX這樣就能夠了,代碼上面已經給出。
在瀏覽器中請求:http://localhost:8080/SpringMVC4View/xStream/view/doXMLXStream.do
結果以下:
<com.hoo.entity.AccountBean><id>1</id><name>haha</name><email>email</email><address>北京</address> <brithday><brithday>2010-11-22</brithday></brithday></com.hoo.entity.AccountBean>
四、 轉換對象數組
代碼以下:
/** * <b>function:</b>轉換對象數組 * @author hoojo * @createDate 2011-4-27 下午06:19:40 * @return */ @RequestMapping("/doMoreXMLXStream") public ModelAndView doMoreXMLXStreamView() { System.out.println("#################ViewController doMoreXMLXStreamView##################"); ModelAndView mav = new ModelAndView("xStreamMarshallingView"); Account[] accs = new Account[2]; Account bean = new Account(); bean.setAddress("北京"); bean.setEmail("email"); bean.setId(1); bean.setName("haha"); Brithday day = new Brithday(); day.setBrithday("2010-11-22"); bean.setBrithday(day); accs[0] = bean; bean = new Account(); bean.setAddress("上海"); bean.setEmail("email"); bean.setId(1); bean.setName("haha"); day = new Brithday(); day.setBrithday("2010-11-22"); bean.setBrithday(day); accs[1] = bean; mav.addObject(accs); return mav; }
在WebBrowser中請求http://localhost:8080/SpringMVC4View/xStream/view/doMoreXMLXStream.do
結果以下:
<myBeans-array><myBeans><id>1</id><name>haha</name><email>email</email> <address>北京</address><生日><brithday>2010-11-22</brithday></生日></myBeans> <myBeans><id>1</id><name>haha</name><email>email</email><address>上海</address> <生日><brithday>2010-11-22</brithday></生日></myBeans></myBeans-array>
結果中的myBeans、生日就是在dispatcher配置文件中重命名的對象屬性名稱。
五、 轉換Map集合
/** * <b>function:</b>轉換Map對象 * @author hoojo * @createDate 2011-4-27 下午06:19:48 * @return */ @RequestMapping("/doDifferXMLXStream") public ModelAndView doDifferXMLXStreamView() { System.out.println("#################ViewController doDifferXMLXStreamView##################"); ModelAndView mav = new ModelAndView("xStreamMarshallingView"); Account bean = new Account(); bean.setAddress("廣東"); bean.setEmail("email"); bean.setId(1); bean.setName("haha"); Brithday day = new Brithday(); day.setBrithday("2010-11-22"); bean.setBrithday(day); User user = new User(); user.setAddress("china GuangZhou"); user.setAge(23); user.setBrithday(new Date()); user.setName("jack"); user.setSex(true); Map<String, Object> map = new HashMap<String, Object>(); map.put("bean", bean); map.put("user", user); mav.addObject(map); return mav; }
在WebBrowser中請求http://localhost:8080/SpringMVC4View/xStream/view/doDifferXMLXStream.do
結果以下:
<map><entry> <string>bean</string><myBeans><id>1</id><name>haha</name><email>email</email><address>廣東</address><生日><brithday>2010-11-22</brithday></生日></myBeans></entry><entry><string>user</string><com.hoo.entity.User><name>jack</name><age>23</age><sex>true</sex><address>china GuangZhou</address><brithday>2011-04-27 19:02:13.747 CST</brithday></com.hoo.entity.User></entry></map>
六、 轉換List集合
/** * <b>function:</b>轉換List對象 * @author hoojo * @createDate 2011-4-27 下午06:20:02 * @return */ @RequestMapping("/doListXMLXStream") public ModelAndView doListXMLXStreamView() { System.out.println("#################ViewController doListXMLXStreamView##################"); ModelAndView mav = new ModelAndView("xStreamMarshallingView"); List<Object> beans = new ArrayList<Object>(); for (int i = 0; i < 3; i++) { Account bean = new Account(); bean.setAddress("北京#" + i); bean.setEmail("email" + i + "@12" + i + ".com"); bean.setId(1 + i); bean.setName("haha#" + i); Brithday day = new Brithday(); day.setBrithday("2010-11-2" + i); bean.setBrithday(day); beans.add(bean); User user = new User(); user.setAddress("china GuangZhou 廣州# " + i); user.setAge(23 + i); user.setBrithday(new Date()); user.setName("jack#" + i); user.setSex(Boolean.parseBoolean(i + "")); beans.add(user); } mav.addObject(beans); return mav; }
在WebBrowser中請求http://localhost:8080/SpringMVC4View/xStream/view/doListXMLXStream.do
結果以下:
<list> <myBeans><id>1</id><name>haha#0</name><email>email0@120.com</email><address>北京#0</address><生日><brithday>2010-11-20</brithday></生日></myBeans><com.hoo.entity.User><name>jack#0</name><age>23</age><sex>false</sex><address>china GuangZhou 廣州# 0</address><brithday>2011-04-27 19:08:40.106 CST</brithday></com.hoo.entity.User><myBeans><id>2</id><name>haha#1</name><email>email1@121.com</email><address>北京#1</address><生日><brithday>2010-11-21</brithday></生日></myBeans><com.hoo.entity.User><name>jack#1</name><age>24</age><sex>false</sex><address>china GuangZhou 廣州# 1</address><brithday>2011-04-27 19:08:40.106 CST</brithday></com.hoo.entity.User><myBeans><id>3</id><name>haha#2</name><email>email2@122.com</email><address>北京#2</address><生日><brithday>2010-11-22</brithday></生日></myBeans><com.hoo.entity.User><name>jack#2</name><age>25</age><sex>false</sex><address>china GuangZhou 廣州# 2</address><brithday>2011-04-27 19:08:40.106 CST</brithday></com.hoo.entity.User></list>
總結,xStream相對jaxb2要簡單些。並且相對比較靈活,能夠輕易的轉換Java普通類型。
下次會介紹castor轉換XML、jibx轉換XML、Jackson轉換JSON 以及自定義Jsonlib視圖轉換Json。