views:

21

answers:

1

Hello,

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,!

+1  A: 

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:

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.

Blaise Doughan