JAXB--@XmlElementWrapper注解和泛型一起使用
当java对象的某个属性使用泛型时,普通对象都没问题,但是遇到HashSet这种集合类封装的元素时,就会出现元素内容序列化不出来的问题,详见如下:
一、示例:
第一步:定义java对象
- package step3;
- import javax.xml.bind.annotation.XmlAccessType;
- import javax.xml.bind.annotation.XmlAccessorType;
- import javax.xml.bind.annotation.XmlRootElement;
- @XmlRootElement
- @XmlAccessorType(value = XmlAccessType.PROPERTY)
- public class Customer<T> {
- String name;
- int age;
- int id;
- T t;
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public int getAge() {
- return age;
- }
- public void setAge(int age) {
- this.age = age;
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- @Override
- public String toString() {
- return "Customer [id=" + id + ",name=" + name + ",age=" + age + ",t=" + t + "]";
- }
- public T getT() {
- return t;
- }
- public void setT(T t) {
- this.t = t;
- }
- }
- package step3;
- import javax.xml.bind.annotation.XmlAccessType;
- import javax.xml.bind.annotation.XmlAccessorType;
- @XmlAccessorType(value = XmlAccessType.PROPERTY)
- public class Book {
- private String id;
- private String name;
- private float price;
- public String getId() {
- return id;
- }
- public void setId(String id) {
- this.id = id;
- }
- public String getName() {
- return name;
- }
- public void setName(String name) {
- this.name = name;
- }
- public float getPrice() {
- return price;
- }
- public void setPrice(float price) {
- this.price = price;
- }
- @Override
- public String toString() {
- return "Book [id=" + id + ",name=" + name + ",price=" + price + "]";
- }
- }
第二步:编组(JAXBContext.newInstance(Customer.class,HashSet.class);方法添加了
HashSet的class对象,以提供给JAXBContext使用。)
- package step3;
- import java.io.File;
- import java.util.HashSet;
- import javax.xml.bind.JAXBContext;
- import javax.xml.bind.JAXBException;
- import javax.xml.bind.Marshaller;
- //Marshaller
- public class Object2XmlDemo {
- public static void main(String[] args) {
- Customer<HashSet<Book>> customer = new Customer<HashSet<Book>>();
- customer.setId(100);
- customer.setName("suo");
- customer.setAge(29);
- Book book = new Book();
- book.setId("1");
- book.setName("哈里波特");
- book.setPrice(100);
- Book book2 = new Book();
- book2.setId("2");
- book2.setName("苹果");
- book2.setPrice(50);
- HashSet<Book> bookSet = new HashSet<Book>();
- bookSet.add(book);
- bookSet.add(book2);
- customer.setT(bookSet);
- try {
- File file = new File("C:\\file1.xml");
- JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class,
- HashSet.class);
- Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
- // output pretty printed
- jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
- jaxbMarshaller.marshal(customer, file);
- jaxbMarshaller.marshal(customer, System.out);
- } catch (JAXBException e) {
- e.printStackTrace();
- }
- }
- }
得到的xml:
- <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
- <customer>
- <age>29</age>
- <id>100</id>
- <name>suo</name>
- <t xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="hashSet"/>
- </customer>
注:
1.泛型使用集合元素替代时,泛型属性所对应的xml没有序列化出来。
2.若JAXBContext.newInstance(Customer.class,HashSet.class);不添加HashSet
的class对象,则报错:
[javax.xml.bind.JAXBException: class java.util.HashSet nor any of its super class is known to this context.]
解决办法:
第一步:新增HashSet的包装类
Book类和Customer类相关代码均不需要改变,新增一个HashSet的包装类,定义如下:
- package step4;
- import java.util.HashSet;
- import javax.xml.bind.annotation.XmlElement;
- import javax.xml.bind.annotation.XmlElementWrapper;
- public class BookSet {
- private HashSet<Book> bookSet = new HashSet<Book>();
- //仅包含get方法,未包含set方法
- @XmlElementWrapper(name = "bookSet")//该注解非必须,仅是标注集合元素
- @XmlElement(name="book")
- public HashSet<Book> getBookSet() {
- return bookSet;
- }
- public void addBook(Book book){
- bookSet.add(book);
- }
- }
注:
1.BookSet类内部使用HashSet实现.
2.BookSet类在get方法上添加了@XmlElementWrapper(name = "bookSet")注解。
第二步:编组
- package step4;
- import java.io.File;
- import javax.xml.bind.JAXBContext;
- import javax.xml.bind.JAXBException;
- import javax.xml.bind.Marshaller;
- //Marshaller
- public class Object2XmlDemo {
- public static void main(String[] args) {
- Customer<BookSet> customer = new Customer<BookSet>();
- customer.setId(100);
- customer.setName("suo");
- customer.setAge(29);
- Book book = new Book();
- book.setId("1");
- book.setName("哈里波特");
- book.setPrice(100);
- Book book2 = new Book();
- book2.setId("2");
- book2.setName("苹果");
- book2.setPrice(50);
- BookSet bookSet = new BookSet();
- bookSet.addBook(book);
- bookSet.addBook(book2);
- customer.setT(bookSet);
- try {
- File file = new File("C:\\file1.xml");
- JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class,
- BookSet.class);
- Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
- // output pretty printed
- jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
- jaxbMarshaller.marshal(customer, file);
- jaxbMarshaller.marshal(customer, System.out);
- } catch (JAXBException e) {
- e.printStackTrace();
- }
- }
- }
注:
1.定义Customer对象时,使用包装类,即:
Customer<BookSet> customer = new Customer<BookSet>();
2.JAXBContext调用newInstance()方法时,传入BookSet的class对象,告知BookSet的类型,即:JAXBContext.newInstance(Customer.class,BookSet.class);
得到的xml:
- <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
- <customer>
- <age>29</age>
- <id>100</id>
- <name>suo</name>
- <t xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="bookSet">
- <bookSet>
- <book>
- <id>2</id>
- <name>苹果</name>
- <price>50.0</price>
- </book>
- <book>
- <id>1</id>
- <name>哈里波特</name>
- <price>100.0</price>
- </book>
- </bookSet>
- </t>
- </customer>
JAXB--@XmlElementWrapper注解和泛型一起使用的更多相关文章
- Android高效率编码-findViewById()的蜕变-注解,泛型,反射
Android高效率编码-findViewById()的蜕变-注解,泛型,反射 Android的老朋友findViewById()篇! 先看看他每天是在干什么 //好吧,很多重复的,只不过想表达项目里 ...
- JAXB常用注解讲解(超详细)
简介: JAXB(Java Architecture for XML Binding) 是一个业界的标准,是一项可以根据XML Schema产生Java类的技术.该过程中,JAXB也提供了将XML实例 ...
- java 反射,注解,泛型,内省(高级知识点)
Java反射 1.Java反射是Java被视为动态(或准动态)语言的一个关键性质.这个机制允许程序在运行时透过Reflection APIs 取得任何一个已知名称的class的内部信息, 包括 ...
- java 结合反射、泛型、注解获取泛型对象
由于自己也不是特别的理解,不能做过多的解释,因为这些是问过老师做出来的,我还没有学到这里.如果有解释错误的 指出来我改正一下.见谅~(如果学到这里了,会完善) 工具类(SQLUtil)核心代码: pa ...
- JAVA 注解,泛型,反射获取泛型,并实例化
JAVA 的泛型加大了 编程的灵活性,在配合上反射,可以让我们省去大量的重复代码,当你用 SpringBoot 整合 JPA 的时候 你会发现,你的 DAO 层只需要继承 BaseDao,在显示标明泛 ...
- 基于java容器注解---基于泛型的自动装配
上面利用@Configuration和@Bean配置bean,中间利用@Autowired,指定s1和s2分别指向哪个实现类,下面利用@Autowired,指定s中只有Integer实现类 例子: 在 ...
- HowToDoInJava 其它教程 2 · 翻译完毕
原文:HowToDoInJava 协议:CC BY-NC-SA 4.0 欢迎任何人参与和完善:一个人可以走的很快,但是一群人却可以走的更远. ApacheCN 学习资源 目录 JMS 教程 JMS 教 ...
- JAXB注解的使用详解
前言: 最近一直在做各种接口的对接,接触最多的数据类型就是JSON和XML数据,还有XML中包含JSON的数据,而在Java中对象和XML之间的转换经常用到JAXB注解,抽空在这里总结一下,首先做一下 ...
- 当Jaxb遇到泛型
前言: 最近的工作内容跟银行有些交互, 对方提供的数据格式采用xml(不是预期的json/protobuf). 为了开发方便, 需要借助jaxb来实现xml和java对象之间的映射. 它还是有点像ja ...
随机推荐
- scons, cmake, bazel
http://scons.org/doc/production/HTML/scons-user/index.html https://github.com/PaddlePaddle/Paddle/is ...
- SpringMVC学习笔记六:使用 hibernate-validator注解式数据校验
对客户端传过来的参数,在使用前一般需要进行校验. SpringMVC框架内置了Validator验证接口,但是实现起来太麻烦.我们一般使用 hibernate-validator进行数据校验. 1:j ...
- Java继承Exception自定义异常类教程以及Javaweb中用Filter拦截并处理异常
转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6403033.html 在项目中的应用见: https://github.com/ygj0930/CoupleS ...
- C++11中的mutex, lock,condition variable实现分析
本文分析的是llvm libc++的实现:http://libcxx.llvm.org/ C++11中的各种mutex, lock对象,实际上都是对posix的mutex,condition的封装.不 ...
- java 生成二维码后叠加LOGO并转换成base64
1.代码 见文末推荐 2.测试 测试1:生成base64码 public static void main(String[] args) throws Exception { String dat ...
- java 解析excel工具类
java 解析excel工具类 CreateTime--2018年3月5日16:48:08 Author:Marydon ReadExcelUtils.java import java.io.Fi ...
- VREP中使用其它Lua库
VREP中的Regular API中有一些矩阵操作的函数,不过有时候还是不能满足计算需求,这时就需要在VREP中使用其它科学计算库(或者用Python/MATLAB之类的外部程序控制). 在这里下载L ...
- 双足步行机器人的ZMP与CoP检测
静态步行与动态步行 机器人步态分为静态步行和动态步行.当机器人做静态步行运动时,身体的各个部分运动速度很小,机器人的整体稳定性较易控制.静态步行稳定性采用机器人的重心地面投影点(Center of G ...
- dom4j 输出UTF-8 XML时中文乱码
使用DOM4J的XMLWriter输出UTF-8编码的XML文件时,出现乱码 public static void writToXml(Document document) throws IOExce ...
- DOM4J对于XML的用法
一.基本使用方式 语法 1.获取根元素 Element root = document.getRootElement(); 2.获取某个元素下的子元素 Element db_e ...