Java 可以编写泛型XmlAdapter吗

Java 可以编写泛型XmlAdapter吗,java,xml,generics,jaxb,Java,Xml,Generics,Jaxb,我知道,我可以使用原始类型来编写XMLAdapter,但我可以使用泛型类型。我试图阅读API(),但甚至没有注意到这方面的任何线索 例如地图: 我想使用,比如: @XmlJavaTypeAdapter(GenericMapAdapter<String, Double>.class)// private final HashMap<String, Double> depWageSum = // new HashMap<String, Double>(); @X

我知道,我可以使用原始类型来编写XMLAdapter,但我可以使用泛型类型。我试图阅读API(),但甚至没有注意到这方面的任何线索

例如地图: 我想使用,比如:

@XmlJavaTypeAdapter(GenericMapAdapter<String, Double>.class)//
private final HashMap<String, Double> depWageSum = //
new HashMap<String, Double>();
@XmlJavaTypeAdapter(GenericMapAdapter.class)//
私有最终HashMap depWageSum=//
新的HashMap();
得到

<depWageSum>
    <entry key="RI">289.001</entry>
    <entry key="VT">499.817</entry>
    <entry key="HI">41.824</entry>
    ...
<depWageSum>

289.001
499.817
41.824
...
而类本身可能会出现以下情况:

@SuppressWarnings("serial") public class GenericMapAdapter<K, V> extends XmlAdapter<GenericMapAdapter.MapType<K, V>, Map<K, V>> {
    public static class MapType<K, V> {
        @XmlValue protected final List<MapTypeEntry<K, V>> entry = new ArrayList<MapTypeEntry<K, V>>();
        public static class MapTypeEntry<K, V> {
            @XmlAttribute protected K key;
            @XmlValue protected V value;
            
            private MapTypeEntry() {};
            public static <K, V> MapTypeEntry<K, V> of(final K k, final V v) {
                return new MapTypeEntry<K, V>() {{this.key = k; this.value = v;}};
    }   }   }
    @Override public Map<K, V> unmarshal(final GenericMapAdapter.MapType<K, V> v) throws Exception {
        return new HashMap<K, V>() {{ for (GenericMapAdapter.MapType.MapTypeEntry<K, V> myEntryType : v.entry)
                    this.put(myEntryType.key, myEntryType.value);}};
    }
    @Override public MapType<K, V> marshal(final Map<K, V> v) throws Exception {
        return new GenericMapAdapter.MapType<K, V>() {{for (K key : v.keySet())
                    this.entry.add(MapTypeEntry.of(key, v.get(key)));}};
}   }
@SuppressWarnings(“串行”)公共类GenericMapAdapter扩展了XmlAdapter{
公共静态类MapType{
@XmlValue protected final List entry=new ArrayList();
公共静态类MapTypeEntry{
@xmlk密钥的属性保护;
@XmlValue保护V值;
私有MapTypeEntry(){};
公共静态地图类型输入(最终K、最终V){
返回新的MapTypeEntry(){{this.key=k;this.value=v;};
}   }   }
@重写公共映射解组(最终GenericMapAdapter.MapType v)引发异常{
返回新的HashMap(){{for(GenericMapAdapter.MapType.MapTypeEntry myEntryType:v.entry)
this.put(myEntryType.key,myEntryType.value);};
}
@重写公共映射类型封送(最终映射v)引发异常{
返回新的GenericMapAdapter.MapType(){{for(K键:v.keySet())
add(MapTypeEntry.of(key,v.get(key));};
}   }

您将无法按说明执行此操作。该类不会保留类型参数。但是,您可以引入一些简单的子类来利用GenericMapAdapter中的逻辑:

public class StringDoubleMapAdapter extends GenericMapAdapter<String, Double> {
}
公共类StringDoubleMapAdapter扩展了GenericMapAdapter{
}
然后在属性上使用适配器子类:

@XmlJavaTypeAdapter(StringDoubleMapAdapter.class)//
private final HashMap<String, Double> depWageSum = //
new HashMap<String, Double>();
@XmlJavaTypeAdapter(StringDoubleMapAdapter.class)//
私有最终HashMap depWageSum=//
新的HashMap();
有关XmlAdapter的更多信息,请参阅:


我将尝试在周一发布一个例子。是的,请-两年后,它仍然会有帮助:-)甚至四年后