欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

springMarchal集成xStream的完整示例代码

时间:2023-01-06 09:03:45|栏目:JAVA代码|点击:

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

上一篇:实战分布式医疗挂号通用模块统一返回结果异常日志处理

栏    目:JAVA代码

下一篇:springboot2.0整合logback日志的详细代码

本文标题:springMarchal集成xStream的完整示例代码

本文地址:http://www.codeinn.net/misctech/223053.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有