【问题标题】:Xml transient not working jaxb(Moxy).?Xml 瞬态不工作 jaxb(Moxy).?
【发布时间】:2013-06-28 06:30:06
【问题描述】:

Xml 瞬态注释不适用于以下模型-

@XmlRootElement
public class JdfValidation {
private String name;
private String dataType;
private String errorMessage;
private String javaValidationLogic;
protected String displayName;
private boolean isCustom;
private List<ValidationInputParam> validationInputParams = new ArrayList<ValidationInputParam>();
public IFile container;

public JdfValidation() {

}

public JdfValidation(String name, String displayName, boolean isCustom) {
    this.name = name;
    this.displayName = displayName;
    this.isCustom = isCustom;
}

@XmlTransient
public IFile getContainer() {
    return container;
}

public void setContainer(IFile container) {
    this.container = container;
}

/**
 * @return the validationInputParams
 */
@XmlElement
public List<ValidationInputParam> getValidationInputParams() {
    return validationInputParams;
}

/**
 * @param validationInputParams
 *            the validationInputParams to set
 */
public void setValidationInputParams(
        List<ValidationInputParam> validationInputParams) {
    this.validationInputParams = validationInputParams;
}

@XmlAttribute
public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

@XmlAttribute
public String getDataType() {
    return dataType;
}

public void setDataType(String dataType) {
    this.dataType = dataType;
}

@XmlAttribute
public String getErrorMessage() {
    return errorMessage;
}

public void setErrorMessage(String errorMessage) {
    this.errorMessage = errorMessage;
}

@XmlElement(name = "logic")
public String getJavaValidationLogic() {
    return javaValidationLogic;
}

public void setJavaValidationLogic(String javaValidationLogic) {
    this.javaValidationLogic = javaValidationLogic;
}

@XmlAttribute
public String getDisplayName() {
    return displayName;
}

public void setDisplayName(String displayName) {
    this.displayName = displayName;
}

@XmlAttribute
public boolean isCustom() {
    return isCustom;
}

public void setCustom(boolean isCustom) {
    this.isCustom = isCustom;
}

}

我也尝试了@XmlAccessorType(XmlAccessType.NONE) 但仍然是相同的异常,以上适用于默认的 jaxb 实现。请帮助。

原因:异常 [EclipseLink-50089] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.JAXBException 异常描述:Java 接口 org.eclipse.core.resources.IFile 不能被 JAXB 映射,因为它有多个可映射的父接口。不支持多重继承

【问题讨论】:

    标签: xml jaxb moxy xmltransient


    【解决方案1】:

    更新

    我们已修复此问题背后的错误(请参阅:http://bugs.eclipse.org/411993),它将在 2013 年 7 月 4 日开始在 EclipseLink 2.5.1 和 2.6.0 流中提供。您可以从以下链接下载每晚构建:


    问题

    如果类型是具有多个超级接口的接口,则当 MOXy 被告知(@XmlTransient)忽略属性时,似乎存在错误。

    1 超级界面 - 工作

    public interface IFile extends IFoo  {
    
    }
    

    超过 1 个超级界面 - 不起作用

    public interface IFile extends IFoo, IBar {
    
    }
    

    您可以使用以下错误来跟踪我们在此问题上的进展。


    解决方法

    您可以使用 MOXy 的外部映射文档来覆盖 IFile 的超类型以使您的用例工作(请参阅:http://blog.bdoughan.com/2010/12/extending-jaxb-representing-annotations.html):

    oxm.xml

    <?xml version="1.0"?>
    <xml-bindings
        xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/oxm"
        package-name="forum17399333">
        <java-types>
            <java-type name="IFile" super-type="java.lang.Object"/>
        </java-types>
    </xml-bindings>
    

    演示

    import java.util.*;
    import javax.xml.bind.JAXBContext;
    import org.eclipse.persistence.jaxb.JAXBContextProperties;
    
    public class Demo {
    
        public static void main(String[] args) throws Exception {
            Map<String, Object> properties = new HashMap<String, Object>(1);
            properties.put(JAXBContextProperties.OXM_METADATA_SOURCE, "forum17399333/oxm.xml");
            JAXBContext jc = JAXBContext.newInstance(new Class[] {JdfValidation.class}, properties);
        }
    
    }
    

    【讨论】:

      【解决方案2】:

      另一种解决方法可能是 -

      @XmlRootElement(name = "Validator")
      public class JdfValidation {
      
      
      private Object container; //Cast this to appropriate type
      
      @Transient
      public Object getContainer() {
          return container;
      }
      
      public void setContainer(Object container) {
          this.container = container;
      }
      }
      

      演示-

      public class Demo {
      
      public static void main(String[] args) throws Exception {
          JAXBContext jc = JAXBContext.newInstance(JdfValidation.class);
      
          JdfValidation jdfValidation  = new JdfValidation();
                  IFile file=getFile();
                  jdfValidation.setContainer(file);
                  Marshaller marshaller = jc.createMarshaller();
          marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
          marshaller.marshal(jdfValidation, System.out);
      }
      
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-05-15
        • 2012-04-06
        • 1970-01-01
        • 1970-01-01
        • 2012-12-18
        • 1970-01-01
        相关资源
        最近更新 更多