Annotations @XmlElement, @XmlRootElement @XmlTransient

本文探讨了在JavaBean中使用@XmlTransient和@XmlElement注解来控制XML序列化的过程。通过具体示例说明如何避免字段映射到属性(UserId),以及如何确保特定的getter方法能正确地被解析为XML元素。
//can prevent the field map to java bean property(UserId) 
@XmlTransient 
public String getAuthorId() {
 return authorId;
 }

//@XmlElement annotation can be used with the following program elements: 
// a JavaBean property
 // non static, non transient field 
// within XmlElements 
//Generally, if a class is annotated with @XmlRootElement, all the getters can be parsed to a child element of the class element.
//Sometimes, even if the getter is a method in a class that is annotated with @XmlRootElement, it may not be parsed to xml element. 
//Adding this annotation can solve the problem.
@XmlElement//(required=true) 
//@JsonSerialize(using=JsonDateSerializer.class) 
public Date getPublishTime() { 
    return publishTime; 
}

//The @XmlRootElement annotation can be used with the following program elements: 
//a top level class 
//an enum type
@XmlRootElement(name = "Book")
 public class Book{} 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值