JAXB忽略没有注释的瞬态字段

时间:2013-03-18 14:41:27

标签: java xml jaxb

我在基于JDK 5的应用程序上使用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[...]
}

1 个答案:

答案 0 :(得分:1)

如果不在字段上使用@XmlTransient注释,我认为无法做到这一点。

您可以做的唯一真正的自定义是在XSD中使用绑定文件或内联绑定。

检查参考资料:http://docs.oracle.com/cd/E17802_01/webservices/webservices/docs/1.5/tutorial/doc/JAXBUsing4.html

相关问题