JAXB--@XmlElementWrapper注解和泛型一起使用(三)
来源:互联网 发布:php reset current 编辑:程序博客网 时间:2024/04/29 12:32
一、示例:
第一步:定义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;}@Overridepublic 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;}@Overridepublic String toString() {return "Book [id=" + id + ",name=" + name + ",price=" + price + "]";}}
第二步:编组(JAXBContext.newInstance(Customer.class,HashSet.class);方法添加了
HashSet的class对象,以提供给JAXBContext使用。)
得到的xml:
注:
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.BookSet类内部使用HashSet实现.
2.BookSet类在get方法上添加了@XmlElementWrapper(name = "bookSet")注解。
第二步:编组
注:
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注解和泛型一起使用(三)
- JAXB--@XmlElementWrapper注解和泛型一起使用(三)
- JAXB--@XmlElementWrapper注解(二)
- JAXB--@XmlElementWrapper注解(二)
- JAXB--@XmlElementWrapper注解(二)
- JAXB 中的@XmlElementWrapper注解生成问题
- JAXB注解使用
- JAXB注解使用
- JAXB注解使用
- JAXB注解使用
- JAXB注解使用
- JAXB注解使用小结
- JAXB注解的使用详解
- JAXB注解的使用详解
- @XmlElementWrapper
- JAXB注解
- jaxb注解
- JAXB注解
- proc文件系统
- C++ - 函数模板(function template) 的 重载(overload) 详解 及 代码
- c/c++ 实现curl http地址
- header函数的作用
- 第14周项目一输出20个数,每5个换行
- JAXB--@XmlElementWrapper注解和泛型一起使用(三)
- 子makefile怎么向总控makefile传递变量
- mysql的调整
- 对数组的数进行排序
- mysql导入导出问题
- MySQL my.cnf参数配置优化详解
- CCTouch的单点触摸与精灵拖动(1)
- JAXB--@XmlType注解标注xml生成顺序(四)
- XMPP协议的原理介绍