Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First of all I would suggest doing such preprocessing before marshalling. It would be much easier. However if it is not possible for some reason then you can create you custom type adapter. Then you can put <code>@XmlJavaTypeAdapter(VersioningAdapter.class)</code> on every type that you want to have versioning enabled. <code>@XmlJavaTypeAdapter</code> can also be specified on package level, but you have to specify to which types it applies. You cannot use <code>XmlAdapter</code> without specifying somewhere <code>@XmlJavaTypeAdapter</code>.</p> <p>Drawbacks of such solution:</p> <ul> <li>if you have multiple versioned types then each of them has to be annotated with <code>@XmlJavaTypeAdapter</code></li> <li><code>@XmlJavaTypeAdapter</code> does not work for root element, only on child elements. You have to call adapter manually on root element before marshalling</li> </ul> <p>AFAIK there is no other option for customizing JAXB marshalling. That's why I think that annotation processing should be performed in separate step before marshalling. Unless you can accept mentioned limitations.</p> <p>Sample adapter (full code can be found <a href="https://github.com/destin/SO-answers/tree/master/SO-jaxb-marshalling-with-custom-annotations" rel="nofollow">here</a>):</p> <pre><code>public class VersioningAdapter extends XmlAdapter&lt;Object, Object&gt; { @Override public Object unmarshal(Object v) throws Exception { // TODO Auto-generated method stub return null; } @Override public Object marshal(Object v) throws Exception { if (v == null) { return v; } Field[] fields = v.getClass().getDeclaredFields(); for (Field field : fields) { Annotation[] annotations = field.getDeclaredAnnotations(); CustomVersion annotation = findCustomVersion(annotations); if (annotation != null) { if (!contains(annotation, Configuration.getVersion())) { field.setAccessible(true); field.set(v, null); } } } return v; } private CustomVersion findCustomVersion(Annotation[] annotations) { for (Annotation annotation : annotations) { if (annotation instanceof CustomVersion) { return (CustomVersion) annotation; } } return null; } private boolean contains(CustomVersion annotation, String version) { String[] values = annotation.value(); for (String value : values) { if (value.equals(version)) { return true; } } return false; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload