2013-03-18 40 views
2

我在基于JDK 5的应用程序上使用JAXB。JAXB忽略没有注释的瞬态字段

XML编组是一个侧面特征,所以POJO模型上的注释被排除。应该排除的字段是transient(java关键字)。

有没有办法配置Marshaler忽略这些字段。

下面是我用连载我的POJO到XML代码:

JAXBContext context = JAXBContext.newInstance(BasePOJO.class, target.getClass()); 

JAXBElement<WsResponse> model = new JAXBElement<BasePOJO>(
     new QName(target.getClass().getSimpleName()), 
     (Class<BasePOJO>) target.getClass(), 
     (BasePOJO)target 
    ); 

Marshaller marshaller = context.createMarshaller(); 
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
marshaller.marshal(model, os); 

样本POJO我需要序列:

public class APOJO extends BasePOJO { 
    private Long id; 
    private String desc; 
    private transient String aFieldToIgnore; 

    //and the accessors[...] 
} 

回答