简体   繁体   中英

JAXB Generic XmlAdapter implementation

This question continues my previous thread : here

Now i want to use a generic XmlAdapter, because i need to apply the same work on differents type of value. I can create one class per type, but that's not the purpose of my question, i want to make this generic. So this is what i did :

Generic Adapter Class

public class GenericXMLAdapter<T> extends XmlAdapter<GenericXMLAdapter.AdaptedValue<T>, T>{
  public static class AdaptedValue<T> {
        @XmlAttribute
        public T code;
  }

  @Override
  public T unmarshal(AdaptedValue<T> v) throws Exception {
        return v.code;
  }

  @Override
  public AdaptedValue<T> marshal(T v) throws Exception {
        AdaptedValue<T> adaptedValue = new AdaptedValue<T>();
        adaptedValue.code = v;
        return adaptedValue;
  }

}

My temporary class to generate the correct Adapter

public final class DefinedXMLAdapter {
      public static class BooleanAdapter extends GenericXMLAdapter<Boolean> {};
}

Example to marshall

  @XmlElement(name = "theBoolean")
  @XmlJavaTypeAdapter(DefinedXMLAdapter.BooleanAdapter.class)
  protected Boolean myBoolean = false;

When i execute my code, i get a weird error i can't understand :

Exception in thread "main" java.lang.NullPointerException
  at com.sun.xml.internal.bind.v2.runtime.reflect.TransducedAccessor.get(TransducedAccessor.java:154)
  ...

Can someone explains to me what's wrong with my code and how to resolve this issue ?

I finally managed to solve my problem by using MOXy as the JAXB implementation. Thanks for your time, have a good day!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM