springMarchal集成xStream的完整示例代碼
Xstream是一個庫, 用於將對象序列化為xml, 反之亦然, 而無需任何映射文件。註意, castor需要一個映射文件。
XStreamMarshaller類提供瞭將對象封送為xml的工具, 反之亦然。
1.maven GAV(1.4.11.1版本才兼容):
<dependency> <groupId>com.thoughtworks.xstream</groupId> <artifactId>xstream</artifactId> <version>1.4.11.1</version> </dependency>
2.POJO類:
package com.yang.alibabamysql.domain.bo; import com.thoughtworks.xstream.annotations.XStreamAlias; import lombok.Data; import java.util.List; @XStreamAlias(value = "man", impl = Man.class) @Data public class Man { @XStreamAlias("id") private Long id; @XStreamAlias("name") private String name; @XStreamAlias("sonList") protected List<Son> sonList; @XStreamAlias(value = "son", impl = Son.class) @Data public static class Son { @XStreamAlias("id") private Long id; @XStreamAlias("name") private String name; } }
3.配置類:
package com.yang.alibabamysql.config; import com.thoughtworks.xstream.io.naming.NoNameCoder; import com.thoughtworks.xstream.io.xml.Xpp3Driver; import com.yang.alibabamysql.domain.bo.Man; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.oxm.xstream.XStreamMarshaller; @Configuration public class XStreamMarshallerConfig { @Bean public XStreamMarshaller getXStreamMarshaller() { XStreamMarshaller xStreamMarshaller = new XStreamMarshaller(); xStreamMarshaller.setStreamDriver(new Xpp3Driver(new NoNameCoder())); xStreamMarshaller.setAnnotatedClasses(Man.class, Man.Son.class); xStreamMarshaller.setAutodetectAnnotations(true); return xStreamMarshaller; } }
4.測試類:
package com.yang.alibabamysql.controller; import com.google.common.collect.Lists; import com.thoughtworks.xstream.io.naming.NoNameCoder; import com.thoughtworks.xstream.io.xml.Xpp3Driver; import com.yang.alibabamysql.AlibabaMysqlApplicationTests; import com.yang.alibabamysql.domain.bo.Man; import org.springframework.oxm.xstream.XStreamMarshaller; import java.util.ArrayList; class UserControllerTest extends AlibabaMysqlApplicationTests { private static final XStreamMarshaller xStreamMarshaller; static { xStreamMarshaller = new XStreamMarshaller(); xStreamMarshaller.setStreamDriver(new Xpp3Driver(new NoNameCoder())); xStreamMarshaller.setAnnotatedClasses(Man.class, Man.Son.class); xStreamMarshaller.setAutodetectAnnotations(true); } public static void marshal() { Man.Son son = new Man.Son(); son.setId(2L); son.setName("ypw"); ArrayList<Man.Son> sonArrayList = Lists.newArrayList(son); Man man = new Man(); man.setId(1L); man.setName("yjx"); man.setSonList(sonArrayList); System.err.println(xStreamMarshaller.getXStream().toXML(man)); public static void unmarshal() { String xml = "<man><id>1</id><name>yjx</name><sonList><son><id>2</id><name>ypw</name></son></sonList></man>"; Man man = (Man) xStreamMarshaller.getXStream().fromXML(xml); System.err.println(man.getSonList().get(0).getName()); public static void main(String[] args) { marshal(); System.out.println("============="); unmarshal(); }
輸出:
<man>
<id>1</id>
<name>yjx</name>
<sonList>
<son>
<id>2</id>
<name>ypw</name>
</son>
</sonList>
</man>
=============
ypw
到此這篇關於springMarchal集成xStream的文章就介紹到這瞭,更多相關springMarchal集成xStream內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!
推薦閱讀:
- Java反射機制基礎詳解
- 使用spring動態獲取接口的不同實現類
- SpringBoot萬字爆肝高級配置
- Java中String的JdbcTemplate連接SQLServer數據庫的方法
- 基於Spring depends-on的使用詳解