SpringBoot XStream整合
XStream使用
XStream简介
Xstream是一种OXMapping 技术,是用来处理XML文件序列化的框架,在将JavaBean序列化,或将XML文件反序列化的时候,不需要其它辅助类和映射文件,使得XML序列化不再繁索。Xstream也可以将JavaBean序列化成Json或反序列化,使用非常方便。
XStream使用案例1
- 添加依赖
<!--xml与bean相互转换-->
<dependency><groupId>com.thoughtworks.xstream</groupId><artifactId>xstream</artifactId><version>1.4.10</version>
</dependency>
- 创建没有添加任何XStream注释的实体类
package com.example.demo.entity;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;/*** @author xzy* @date 2020-03-16 21:14* 说明:玩具球*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Ball {/*** 颜色*/private String color;/*** 价格*/private Double price;
}
package com.example.demo.entity;import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;import java.util.List;/*** @author xzy* @date 2020-03-16 21:13* 说明:猫*/
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Cat {/*** 名字*/private String name;/*** 年龄*/private Integer age;/*** 拥有的玩具球*/private List<Ball> balls;
}
- 创建Java对象,使用XStream将对象转换成XML
package com.example.demo.service;import com.example.demo.entity.Ball;
import com.example.demo.entity.Cat;
import com.thoughtworks.xstream.XStream;import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;/*** @author xzy* @date 2020-03-16 21:17* 说明:*/
public class Test {public static void main(String[] args) throws Exception {List<Ball> balls = new ArrayList<>(2);balls.add(new Ball("red", 19.0));balls.add(new Ball("yellow", 30.0));Cat cat = new Cat("胖子", 1, balls);FileOutputStream fileOutputStream = new FileOutputStream("E:\\Program Code\\Java\\xstream\\src\\main\\resources\\cat.xml");XStream xstream = new XStream();xstream.toXML(cat,fileOutputStream);}
}
转换结果为:
<com.example.demo.entity.Cat><name>胖子</name><age>1</age><balls><com.example.demo.entity.Ball><color>red</color><price>19.0</price></com.example.demo.entity.Ball><com.example.demo.entity.Ball><color>yellow</color><price>30.0</price></com.example.demo.entity.Ball></balls>
</com.example.demo.entity.Cat>
XStream使用案例2——@XStreamAlias()注解
- 简单说明
Alias翻译过来就是“化名“、”别名“的意思,确实@XStreamAlias()注解的作用就是为有元素起别名。
- 为实体类添加@XStreamAlias()注解
package com.example.demo.entity;import com.thoughtworks.xstream.annotations.XStreamAlias;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;import java.util.List;/*** @author xzy* @date 2020-03-16 21:13* 说明:猫*/
@Data
@NoArgsConstructor
@AllArgsConstructor
@XStreamAlias("cat")
public class Cat {/*** 名字*/@XStreamAlias("catName")private String name;/*** 年龄*/@XStreamAlias("catAge")private Integer age;/*** 拥有的玩具球*/@XStreamAlias("catBalls")private List<Ball> balls;
}
package com.example.demo.entity;import com.thoughtworks.xstream.annotations.XStreamAlias;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;/*** @author xzy* @date 2020-03-16 21:14* 说明:玩具球*/
@Data
@NoArgsConstructor
@AllArgsConstructor
@XStreamAlias("ball")
public class Ball {/*** 颜色*/@XStreamAlias("ballColor")private String color;/*** 价格*/@XStreamAlias("ballPrice")private Double price;
}
- 调用XStream类的processAnnotations()方法,选择激活哪些类的XStream注解
package com.example.demo.service;import com.example.demo.entity.Ball;
import com.example.demo.entity.Cat;
import com.thoughtworks.xstream.XStream;import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.List;/*** @author xzy* @date 2020-03-16 21:17* 说明:*/
public class Test {public static void main(String[] args) throws Exception {List<Ball> balls = new ArrayList<>(2);balls.add(new Ball("red", 19.0));balls.add(new Ball("yellow", 30.0));Cat cat = new Cat("胖子", 1, balls);FileOutputStream fileOutputStream = new FileOutputStream("E:\\Program Code\\Java\\xstream\\src\\main\\resources\\cat.xml");XStream xstream = new XStream();/*** 需要明确指出,哪个类的XStream注解需要被激活*/xstream.processAnnotations(Ball.class);//xstream.processAnnotations(Cat.class);xstream.toXML(cat, fileOutputStream);}
}
上面的代码只激活了,Ball类中的XStream注解,执行后生成的xml为:
<com.example.demo.entity.Cat><name>胖子</name><age>1</age><balls><ball><ballColor>red</ballColor><ballPrice>19.0</ballPrice></ball><ball><ballColor>yellow</ballColor><ballPrice>30.0</ballPrice></ball></balls>
</com.example.demo.entity.Cat>
可以发现,只有Ball类中的@XStreamAlias()注解起到了作用。现在将Cat类的XStream注解也激活,执行代码,生成的xml为:
<cat><catName>胖子</catName><catAge>1</catAge><catBalls><ball><ballColor>red</ballColor><ballPrice>19.0</ballPrice></ball><ball><ballColor>yellow</ballColor><ballPrice>30.0</ballPrice></ball></catBalls>
</cat>
如果觉得指定开启某类的XStream注解麻烦,可以设置xstream.autodetectAnnotations(true)
XStream使用案例3——@XStreamAsAttribute()注解
- 简单说明
@XStreamAsAttribute用于将类中某个成员作为父节点的属性输出
- 在Cat类中添加注解
package com.example.demo.entity;import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;import java.util.List;/*** @author xzy* @date 2020-03-16 21:13* 说明:猫*/
@Data
@NoArgsConstructor
@AllArgsConstructor
@XStreamAlias("cat")
public class Cat {/*** 名字*/@XStreamAsAttribute//将name变为父节点的属性@XStreamAlias("catName")private String name;/*** 年龄*/@XStreamAlias("catAge")private Integer age;/*** 拥有的玩具球*/@XStreamAlias("catBalls")private List<Ball> balls;
}
- 生成xml
<cat catName="胖子"><catAge>1</catAge><catBalls><ball><ballColor>red</ballColor><ballPrice>19.0</ballPrice></ball><ball><ballColor>yellow</ballColor><ballPrice>30.0</ballPrice></ball></catBalls>
</cat>
XStream使用案例4——@XStreamImplicit()注解
- 简单说明
@XStreamImplict()注解常用于集合类型元素,前面的例子中,Cat类中的ball属性就是一个集合,最终生成的xml中catName、catAge以及catBalls是同级标签,如果我们想去除catBalls标签,将catBalls下的子标签变成与catName和catAge同级,我们就可以使用@XStreamImplict()注解。
- 在Cat类中添加注解
package com.example.demo.entity;import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamImplicit;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;import java.util.List;/*** @author xzy* @date 2020-03-16 21:13* 说明:猫*/
@Data
@NoArgsConstructor
@AllArgsConstructor
@XStreamAlias("cat")
public class Cat {/*** 名字*/@XStreamAlias("catName")private String name;/*** 年龄*/@XStreamAlias("catAge")private Integer age;/*** 拥有的玩具球*/@XStreamImplicit//去除catBalls标签@XStreamAlias("catBalls")private List<Ball> balls;
}
- 生成的xml
<cat><catName>胖子</catName><catAge>1</catAge><ball><ballColor>red</ballColor><ballPrice>19.0</ballPrice></ball><ball><ballColor>yellow</ballColor><ballPrice>30.0</ballPrice></ball>
</cat>
SpringBoot XStream整合相关推荐
- Docker 部署 SpringBoot 项目整合 Redis 镜像做访问计数Demo
Docker 部署SpringBoot项目整合 Redis 镜像做访问计数Demo 最终效果如下 大概就几个步骤 1.安装 Docker CE 2.运行 Redis 镜像 3.Java 环境准备 4. ...
- springboot+security整合(1)
说明 springboot 版本 2.0.3 源码地址:点击跳转 系列 springboot+security 整合(1) springboot+security 整合(2) springboot+s ...
- SpringBoot服务整合(整合邮件服务、定时调度、Actuator监控)
在进行项目开发的时候经常会遇见以下的几个问题:需要进行邮件发送.定时的任务调度.系统的监控处理,实际上这些操 作都可以通过 SpringBoot 进行整合操作.2.1.SpringBoot 整合邮件服 ...
- springboot下整合各种配置文件
本博是在springboot下整合其他中间件,比如,mq,redis,durid,日志...等等 以后遇到再更.springboot真是太便捷了,让我们赶紧涌入到springboot的怀抱吧. ap ...
- springboot项目整合mybatis
SpringBoot项目整合mybatis 本章内容 使用 idea创建 SpringBoot项目 SpringBoot项目中配制 mybatis 框架 1 创建 SpringBoot项目 1.1 在 ...
- SpringBoot系列九:SpringBoot服务整合(整合邮件服务、定时调度、Actuator监控)
声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅. 1.概念:SpringBoot 服务整合 2.背景 在进行项目开发的时候经常会遇见以下的几个问题:需要进行邮件发送.定时的任务调 ...
- SpringBoot项目整合Retrofit最佳实践,这才是最优雅的HTTP客户端工具!
作者:六点半起床 juejin.im/post/6854573211426750472 大家都知道okhttp是一款由square公司开源的java版本http客户端工具.实际上,square公司还开 ...
- SpringBoot 2 整合 Spring Session 最简操作
SpringBoot 2 整合 SpringSession 前言 Spring Session 介绍 SpringBoot 快速整合 Spring Session Spring Session 测试 ...
- SpringBoot+Swagger整合API
SpringBoot+Swagger整合API Swagger:整合规范的api,有界面的操作,测试 1.在pom.xml加入swagger依赖 <!--整合Swagger2配置类-->& ...
最新文章
- error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup
- wxWidgets:日志概述
- 为什么matlab显示error,【求救】我安装了资源 MATLAB R2012b 后,显示有error……
- 一个go1.9.x 编译器内联引起的栈信息错乱的问题分析
- SQL中使用DISTINCT显示多个字段的方法(不使用DISTINCT了)
- Codeforces Round #256 (Div. 2)
- Json字符串转map集合
- C++socket编程(八)8.3:UDP广播
- 【Swift 4.0】扩展 WCDB 支持 SQL 语句
- PyGame:Python 游戏编程入门-1
- 脑电EEG代码开源分享 【1.前置准备-静息态篇】
- C语言输出大写金额,编程实现,输入一个人民币小写金额值,转化为大写金额值输出。先实现基本功能...
- Android实例开发中按钮(Button)的四种点击方式的实现
- 【论文学习】《On Prosody Modeling For ASR+TTS Based Voice Conversion》
- 帆软实现分页时第一行和最后两行冻结方式
- 计算机电源风扇安装方法,机箱风扇怎么装 电脑机箱风扇电源线接法
- python实现微信付款码支付(刷卡支付)(纯python)
- V社线下沙龙·深圳站——12.05(周六)
- ESP12f/E(8266)以及STM32串口自动烧录电路
- CSharp中集合与字典Contains效率差别
热门文章
- 人工智能书单|Deep Learning on Graphs
- Python YouTube频道的终极清单
- 编程:5 位评委对参赛选手进行打分,将所有的打分结果存储在对应类型的数组中,将所有评分结果 去除一个最低分,去除一个最高分,然后获取剩余 3 位评委的平均分数为选手的最终得分。设计程序, 用键盘输入
- WT588F34B语音芯片单曲更换语音功能的实现与应用
- 使用osmconvert转换pbf文件至osm文件
- H5中获取微信头像、昵称
- Linux 文件服务系统
- debussy和nlint常用快捷键
- html在文字中加超链接,html文字加超链接设置
- 关于西数黑盘那些事 原来这样滴 小伙伴们都惊呆了