jaxb - @XmlPath (".") 与@XmlAdapter 冲突

标签 jaxb moxy

有了这个 Jaxb Xml 定义,我尝试通过添加 @XmlPath(".") 来删除 Map Elements Wrapper但在解码期间会导致异常

@XmlRootElement
public abstract class ViewElement{
    @XmlJavaTypeAdapter(value=EventAdapter.class)   
    public Map<Event, String> getEvents() {     
    }
    private transient Class entityType;
    public Class getEntityType() {
        return entityType;
    }
}

EventAdapter 是

public class EventAdapter extends XmlAdapter<EventAdapter.AdaptedMap, Map<Event, String>> { 
    public static class AdaptedMap {
        @XmlVariableNode("key")
        List<AdaptedEntry> entries = new ArrayList<AdaptedEntry>();
    }
    public static class AdaptedEntry {
        @XmlTransient
        public String key;
        @XmlValue
        public String value;
    }
    .....       
}

我的输出是

<element>
   <events>
      <onCellEdit>do some thing<onCellEdit>
   </events>
   <entityType>com.agitech.erp.model.erp.ErpFolder</entityType>
<element>

我尝试删除 <events>通过添加 @XmlPath(".") 标记

@XmlPath(".")
@XmlJavaTypeAdapter(value=EventAdapter.class)   
public Map<Event, String> getEvents() {     
}

效果不错

<element>
   <onCellEdit>do some thing<onCellEdit>       
   <entityType>com.agitech.erp.model.erp.ErpFolder</entityType>
<element>

但是解码失败了

Caused by: Exception [EclipseLink-3002] (Eclipse Persistence Services - 2.6.0.v20140809-296a69f): org.eclipse.persistence.exceptions.ConversionException
Exception Description: The object [], of class [class java.lang.String], from mapping [org.eclipse.persistence.oxm.mappings.XMLDirectMapping[entityType-->view.entityType/text()]] with descriptor [XMLDescriptor(com.agitech.erp.view.BeanView --> [DatabaseTable(view), DatabaseTable(viewFrame), DatabaseTable(viewElement)])], could not be converted to [class java.lang.Class].
Internal Exception: java.lang.ClassNotFoundException: 
    at org.eclipse.persistence.exceptions.ConversionException.couldNotBeConvertedToClass(ConversionException.java:95)
    at org.eclipse.persistence.internal.helper.ConversionManager.convertObjectToClass(ConversionManager.java:446)

调试 Jaxb 让我上线

org.eclipse.persistence.internal.oxm.XMLDirectMappingNodeValue

public void endElement(XPathFragment xPathFragment, UnmarshalRecord unmarshalRecord) {
    ...
    line 205 unmarshalRecord.setAttributeValue(convertedValue, xmlDirectMapping);
}

在解码 entityType 值期间,UnmarshalRecordImpl.currentObj包含 EventAdapter 而不是父元素

我修改org.eclipse.persistence.internal.oxm.record.UnmarshalRecordImpl

public XPathNode getNonAttributeXPathNode(String namespaceURI, String localName, String qName, Attributes attributes) {
....
    if(null == resultNode && null == nonPredicateNode) {
        // ANY MAPPING
        resultNode = xPathNode.getAnyNode();
// by default it return the EventAdapter, changing it to NULL fix my problem
    }
....
}

不是安全的解决方案

最佳答案

我已经能够重现您遇到的问题,但尚未找出原因。您可以使用以下错误来跟踪此问题的进展:

关于jaxb - @XmlPath (".") 与@XmlAdapter 冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27765087/

相关文章:

java - JAXB 和多对象关系

java - 在 @XmlElementWrapper 上添加属性

java-8 - 如何将 Java 8 可选与 Moxy 和 Jersey 一起使用

java - 如何解决第三方 XML 架构冲突?

java - Glassfish 4 REST XML 工作 JSON 错误

java - MSML 的 XSD 架构中的 XML 失败 (RFC 5707)

jersey - 如何将 JSON 请求发布到 Jersey REST 服务?

java - 如何使用 jaxb/moxy 管理多种 xml 格式

java - Jersey 2.0 相当于 POJOMappingFeature

jaxb 解码器 : repeated xmlelement without wrapper