当java对象的某个属性使用泛型时,普通对象都没问题,但是遇到HashSet这种集合类封装的元素时,就会出现元素内容序列化不出来的问题,详见如下:

一、示例:

第一步:定义java对象

  1. package step3;
  2. import javax.xml.bind.annotation.XmlAccessType;
  3. import javax.xml.bind.annotation.XmlAccessorType;
  4. import javax.xml.bind.annotation.XmlRootElement;
  5. @XmlRootElement
  6. @XmlAccessorType(value = XmlAccessType.PROPERTY)
  7. public class Customer<T> {
  8. String name;
  9. int age;
  10. int id;
  11. T t;
  12. public String getName() {
  13. return name;
  14. }
  15. public void setName(String name) {
  16. this.name = name;
  17. }
  18. public int getAge() {
  19. return age;
  20. }
  21. public void setAge(int age) {
  22. this.age = age;
  23. }
  24. public int getId() {
  25. return id;
  26. }
  27. public void setId(int id) {
  28. this.id = id;
  29. }
  30. @Override
  31. public String toString() {
  32. return "Customer [id=" + id + ",name=" + name + ",age=" + age + ",t=" + t + "]";
  33. }
  34. public T getT() {
  35. return t;
  36. }
  37. public void setT(T t) {
  38. this.t = t;
  39. }
  40. }
  1. package step3;
  2. import javax.xml.bind.annotation.XmlAccessType;
  3. import javax.xml.bind.annotation.XmlAccessorType;
  4. @XmlAccessorType(value = XmlAccessType.PROPERTY)
  5. public class Book {
  6. private String id;
  7. private String name;
  8. private float price;
  9. public String getId() {
  10. return id;
  11. }
  12. public void setId(String id) {
  13. this.id = id;
  14. }
  15. public String getName() {
  16. return name;
  17. }
  18. public void setName(String name) {
  19. this.name = name;
  20. }
  21. public float getPrice() {
  22. return price;
  23. }
  24. public void setPrice(float price) {
  25. this.price = price;
  26. }
  27. @Override
  28. public String toString() {
  29. return "Book [id=" + id + ",name=" + name + ",price=" + price + "]";
  30. }
  31. }

第二步:编组(JAXBContext.newInstance(Customer.class,HashSet.class);方法添加了

HashSet的class对象,以提供给JAXBContext使用。)

  1. package step3;
  2. import java.io.File;
  3. import java.util.HashSet;
  4. import javax.xml.bind.JAXBContext;
  5. import javax.xml.bind.JAXBException;
  6. import javax.xml.bind.Marshaller;
  7. //Marshaller
  8. public class Object2XmlDemo {
  9. public static void main(String[] args) {
  10. Customer<HashSet<Book>> customer = new Customer<HashSet<Book>>();
  11. customer.setId(100);
  12. customer.setName("suo");
  13. customer.setAge(29);
  14. Book book = new Book();
  15. book.setId("1");
  16. book.setName("哈里波特");
  17. book.setPrice(100);
  18. Book book2 = new Book();
  19. book2.setId("2");
  20. book2.setName("苹果");
  21. book2.setPrice(50);
  22. HashSet<Book> bookSet = new HashSet<Book>();
  23. bookSet.add(book);
  24. bookSet.add(book2);
  25. customer.setT(bookSet);
  26. try {
  27. File file = new File("C:\\file1.xml");
  28. JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class,
  29. HashSet.class);
  30. Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
  31. // output pretty printed
  32. jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
  33. jaxbMarshaller.marshal(customer, file);
  34. jaxbMarshaller.marshal(customer, System.out);
  35. } catch (JAXBException e) {
  36. e.printStackTrace();
  37. }
  38. }
  39. }

得到的xml:

  1. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  2. <customer>
  3. <age>29</age>
  4. <id>100</id>
  5. <name>suo</name>
  6. <t xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="hashSet"/>
  7. </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的包装类,定义如下:

  1. package step4;
  2. import java.util.HashSet;
  3. import javax.xml.bind.annotation.XmlElement;
  4. import javax.xml.bind.annotation.XmlElementWrapper;
  5. public class BookSet {
  6. private HashSet<Book> bookSet = new HashSet<Book>();
  7. //仅包含get方法,未包含set方法
  8. @XmlElementWrapper(name = "bookSet")//该注解非必须,仅是标注集合元素
  9. @XmlElement(name="book")
  10. public HashSet<Book> getBookSet() {
  11. return bookSet;
  12. }
  13. public void addBook(Book book){
  14. bookSet.add(book);
  15. }
  16. }

注:

1.BookSet类内部使用HashSet实现.

2.BookSet类在get方法上添加了@XmlElementWrapper(name = "bookSet")注解。

第二步:编组

  1. package step4;
  2. import java.io.File;
  3. import javax.xml.bind.JAXBContext;
  4. import javax.xml.bind.JAXBException;
  5. import javax.xml.bind.Marshaller;
  6. //Marshaller
  7. public class Object2XmlDemo {
  8. public static void main(String[] args) {
  9. Customer<BookSet> customer = new Customer<BookSet>();
  10. customer.setId(100);
  11. customer.setName("suo");
  12. customer.setAge(29);
  13. Book book = new Book();
  14. book.setId("1");
  15. book.setName("哈里波特");
  16. book.setPrice(100);
  17. Book book2 = new Book();
  18. book2.setId("2");
  19. book2.setName("苹果");
  20. book2.setPrice(50);
  21. BookSet bookSet = new BookSet();
  22. bookSet.addBook(book);
  23. bookSet.addBook(book2);
  24. customer.setT(bookSet);
  25. try {
  26. File file = new File("C:\\file1.xml");
  27. JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class,
  28. BookSet.class);
  29. Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
  30. // output pretty printed
  31. jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
  32. jaxbMarshaller.marshal(customer, file);
  33. jaxbMarshaller.marshal(customer, System.out);
  34. } catch (JAXBException e) {
  35. e.printStackTrace();
  36. }
  37. }
  38. }

注:

1.定义Customer对象时,使用包装类,即:

Customer<BookSet> customer = new Customer<BookSet>();

2.JAXBContext调用newInstance()方法时,传入BookSet的class对象,告知BookSet的类型,即:JAXBContext.newInstance(Customer.class,BookSet.class);

得到的xml:

  1. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
  2. <customer>
  3. <age>29</age>
  4. <id>100</id>
  5. <name>suo</name>
  6. <t xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="bookSet">
  7. <bookSet>
  8. <book>
  9. <id>2</id>
  10. <name>苹果</name>
  11. <price>50.0</price>
  12. </book>
  13. <book>
  14. <id>1</id>
  15. <name>哈里波特</name>
  16. <price>100.0</price>
  17. </book>
  18. </bookSet>
  19. </t>
  20. </customer>

JAXB--@XmlElementWrapper注解和泛型一起使用的更多相关文章

  1. Android高效率编码-findViewById()的蜕变-注解,泛型,反射

    Android高效率编码-findViewById()的蜕变-注解,泛型,反射 Android的老朋友findViewById()篇! 先看看他每天是在干什么 //好吧,很多重复的,只不过想表达项目里 ...

  2. JAXB常用注解讲解(超详细)

    简介: JAXB(Java Architecture for XML Binding) 是一个业界的标准,是一项可以根据XML Schema产生Java类的技术.该过程中,JAXB也提供了将XML实例 ...

  3. java 反射,注解,泛型,内省(高级知识点)

     Java反射 1.Java反射是Java被视为动态(或准动态)语言的一个关键性质.这个机制允许程序在运行时透过Reflection APIs    取得任何一个已知名称的class的内部信息, 包括 ...

  4. java 结合反射、泛型、注解获取泛型对象

    由于自己也不是特别的理解,不能做过多的解释,因为这些是问过老师做出来的,我还没有学到这里.如果有解释错误的 指出来我改正一下.见谅~(如果学到这里了,会完善) 工具类(SQLUtil)核心代码: pa ...

  5. JAVA 注解,泛型,反射获取泛型,并实例化

    JAVA 的泛型加大了 编程的灵活性,在配合上反射,可以让我们省去大量的重复代码,当你用 SpringBoot 整合 JPA 的时候 你会发现,你的 DAO 层只需要继承 BaseDao,在显示标明泛 ...

  6. 基于java容器注解---基于泛型的自动装配

    上面利用@Configuration和@Bean配置bean,中间利用@Autowired,指定s1和s2分别指向哪个实现类,下面利用@Autowired,指定s中只有Integer实现类 例子: 在 ...

  7. HowToDoInJava 其它教程 2 · 翻译完毕

    原文:HowToDoInJava 协议:CC BY-NC-SA 4.0 欢迎任何人参与和完善:一个人可以走的很快,但是一群人却可以走的更远. ApacheCN 学习资源 目录 JMS 教程 JMS 教 ...

  8. JAXB注解的使用详解

    前言: 最近一直在做各种接口的对接,接触最多的数据类型就是JSON和XML数据,还有XML中包含JSON的数据,而在Java中对象和XML之间的转换经常用到JAXB注解,抽空在这里总结一下,首先做一下 ...

  9. 当Jaxb遇到泛型

    前言: 最近的工作内容跟银行有些交互, 对方提供的数据格式采用xml(不是预期的json/protobuf). 为了开发方便, 需要借助jaxb来实现xml和java对象之间的映射. 它还是有点像ja ...

随机推荐

  1. 高仿美团主界面&lt;一&gt;

    声明:本demo还未完好,正在持续更新中... 先上图吧: 这个小demo资源图片全是用青花瓷抠出来的,如今仅仅是完毕了 一部分. 会持续更行中. . .有兴趣的朋友能够关注我,我们一起coding, ...

  2. Python学习笔记三:模块

    一:模块 一个模块就是一个py文件,里面定义了一些业务函数.引用模块,可以用import语句导入.导入模块后,通过 模块.函数名(参数)  来使用模块中的函数.如果存在多个同名模块,则前面模块名需要加 ...

  3. 防止跨站请求伪造(CSRF)攻击 和 防重复提交 的方法的实现

    CSRF的概念可以参考:http://netsecurity.51cto.com/art/200812/102951.htm 本文介绍的是基于spring拦截器的Spring MVC实现 首先配置拦截 ...

  4. JUC-Condition和Lock实践-线程按序交替执行

    编写一个程序,开启 3 个线程,这三个线程的 ID 分别为 A.B.C,每个线程将自己的 ID 在屏幕上打印 10 遍,要求输出的结果必须按顺序显示.如:ABCABCABC…… 依次递归 这里只使用c ...

  5. centos7 在docker swarm中运行Jenkins,利用gitlab的webhook触发自动部署脚本

    1.宿主机中创建目录 mkdir -p /jenkins_home 2.编辑compose文件,文件名jenkins.yml version: '3.4' services: jenkins-upgr ...

  6. Centos下和Win7下查看端口占用情况

    Centos #会列出所有正在使用的端口及关联的进程/应用 netstat -nap #portnumber要用具体的端口号代替,可以直接列出该端口听使用进程/应用 lsof -i :portnumb ...

  7. SpannableStringUtil实现丰富文字效果

    代码地址如下:http://www.demodashi.com/demo/15007.html 前言 在android开发中,我们不免会用到 TextView 的各种显示效果,如字体样式,颜色,大小, ...

  8. 分享一个VS2008漂亮的黑色主题

    如果恰巧你也使用Visual Studio 2008这个版本, 如果你也恰巧厌烦了白色的背景, 那么推荐你使用这款纯黑色theme,搭配上DroidSansMonog这个等宽字体(打包下载),每天都可 ...

  9. android软键盘弹出引起的各种不适终极解决方案

    android软键盘弹出引起的各种不适终极解决方案 以下描述如何解决ListView高度小于0时出现的UI问题. 创建RelativeLayout的子类TxrjRelativeLayout publi ...

  10. PHPNow升级PHP版本为5.3.5的方法(转)

    PHPNow升级PHP版本为5.3.5的方法 原文:http://sharebar.org/1142.html 在WIN上有时候需要测试一些PHP程序,又不会自行独立配置环境,那么PHPNow是非常好 ...