当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Can you optionally serialize a class property through JAXB?

问题描述:

Is it possible to optionally serialize the properties of a class via JAX-B using using some dynamic flag?

e.g. Suppose I Have

@XmlRootElement

public class TodoItem {

private int id;

private String title;

private String note;

// getters, setters

}

and the following web service operatios:

 public TodoItem getTodoItemFull(int id) { .... }

public TodoItem getTodoItemMinimal(int id) { .... }

Is there a special annotation I can use so that I can decide at runtime whether property "note" will be serialized? In other words, the getTodoItemFull() method will return the fully serialized class, while the getTodoItemMinimal() method will return that serialized class without the "note" xml element?

Thanks,!

网友答案:

You could do this by applying multiple mappings to your object model. Standard JAXB only allows you to have one mapping applied through annotations. Other JAXB implementations such as MOXy (I lead this implementation), also allow you to represent metadata as XML:

  • http://wiki.eclipse.org/EclipseLink/Examples/MOXy/EclipseLink-OXM.XML

You could have one JAXBContext built on one set of metadata that fully mapped the object model that the "getTodoItemFull" would use, and another JAXBContext built on another set of metadata that partially mapped the object model that getTodoItemMininmal would use.

分享给朋友:
您可能感兴趣的文章:
随机阅读: