XmlAdapter与较新版本的JAXB无法正常工作

我正在使用以下来源执行Maven项目

package com.coderplus.jaxb; import java.util.HashMap; import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; @XmlJavaTypeAdapter(PropertiesMapAdapter.class) public class PropertiesMap extends HashMap { } 

..

 package com.coderplus.jaxb; import java.util.Map.Entry; import javax.xml.bind.annotation.adapters.XmlAdapter; public class PropertiesMapAdapter extends XmlAdapter<Properties, PropertiesMap> { @Override public PropertiesMap unmarshal(Properties properties) throws Exception { PropertiesMap retVal = new PropertiesMap(); if (null != properties) { for (Property param : properties.getProperty()) { retVal.put(param.getName(), param.getValue()); } } return retVal; } @Override public Properties marshal(PropertiesMap propertiesMap) throws Exception { Properties properties = new Properties(); if (propertiesMap != null) { for (Entry entry : propertiesMap.entrySet()) { Property param = new Property(); param.setName(entry.getKey()); param.setValue(entry.getValue()); properties.getProperty().add(param); } } return properties; } } 

..

 package com.coderplus.jaxb; import javax.xml.bind.*; public class Demo { @SuppressWarnings("unchecked") public static void main(String[] args) throws Exception { JAXBContext jc = JAXBContext.newInstance(Root.class); Root root = new Root(); PropertiesMap map = new PropertiesMap(); map.put("hello", "World"); map.put("name", "value"); root.setProperties(map); Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(root, System.out); } } 

src / main / resources中的模式

                       

在src / main / resources中绑定文件

          

最后是pom文件

  4.0.0 com.coderplus.jaxb test 0.0.1-SNAPSHOT    org.jvnet.jaxb2.maven2 maven-jaxb2-plugin 0.9.0    generate     com.coderplus.jaxb      

当我使用JDK 1.6执行它时,Demo Class产生以下输出

    World value   

但出于某种原因,它使用JDK 1.7及更高版本(较新的JAXB?)生成以下内容

     hello World   name value    

我怎样才能使它在JDK 1.7或更新版本的JAXB上运行?

更多信息:

maven-jaxb2-plugin与其他类一起生成以下类

 @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "", propOrder = { "properties" }) @XmlRootElement(name = "Root") public class Root { @XmlElement(name = "Properties", required = true, type = Properties.class) protected PropertiesMap properties; public PropertiesMap getProperties() { return properties; } public void setProperties(PropertiesMap value) { this.properties = value; } } 

如果我手动进入并添加注释@XmlJavaTypeAdapter(PropertiesMapAdapter.class)如下面的代码,那么它的工作原理

 @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "", propOrder = { "properties" }) @XmlRootElement(name = "Root") public class Root { @XmlElement(name = "Properties", required = true, type = Properties.class) @XmlJavaTypeAdapter(PropertiesMapAdapter.class) protected PropertiesMap properties; public PropertiesMap getProperties() { return properties; } public void setProperties(PropertiesMap value) { this.properties = value; } } 

如何让maven-jaxb2-plugin自动添加XmlJavaTypeAdapter ? 万一它会有帮助,这个链接有拉链Maven项目

尝试添加xjc:javaType自定义。