springMarchal集成xStream的完整示例代码

Xstream是一个库, 用于将对象序列化为xml, 反之亦然, 而无需任何映射文件。注意, castor需要一个映射文件。

XStreamMarshaller类提供了将对象封送为xml的工具, 反之亦然。

1.maven GAV(1.4.11.1版本才兼容):


    com.thoughtworks.xstream
    xstream
    1.4.11.1

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 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 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 = "1yjx2ypw";
        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();
}

输出:


  1
  yjx
 
   
      2
      ypw
   

 


=============
ypw

到此这篇关于springMarchal集成xStream的文章就介绍到这了,更多相关springMarchal集成xStream内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

你可能感兴趣的:(springMarchal集成xStream的完整示例代码)