Xml transient not working jaxb(Moxy).?

感情迁移 提交于 2019-12-12 18:33:16

问题


Xml transient annotation not working for following model-

@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;
}

}

I also tried @XmlAccessorType(XmlAccessType.NONE) bu still same exception,above works with default jaxb implementation.Plz,help.

Caused by: Exception [EclipseLink-50089] (Eclipse Persistence Services - 2.5.0.v20130507-3faac2b): org.eclipse.persistence.exceptions.JAXBException Exception Description: The java interface org.eclipse.core.resources.IFile can not be mapped by JAXB as it has multiple mappable parent interfaces. Multiple inheritence is not supported


回答1:


UPDATE

We have fixed the bug behind this issue (see: http://bugs.eclipse.org/411993) it will be available in the EclipseLink 2.5.1 and 2.6.0 streams starting on July 4, 2013. You can download a nightly build from the following link:

  • http://www.eclipse.org/eclipselink/downloads/nightly.php

ISSUE

There appears to be a bug when MOXy is told (with @XmlTransient) to ignore a property if the type is an interface with multiple super interfaces.

1 Super Interface - Works

public interface IFile extends IFoo  {

}

More Than 1 Super Interface - Doesn't Work

public interface IFile extends IFoo, IBar {

}

You can use the following bug to track our progress on this issue.

  • http://bugs.eclipse.org/411993

WORKAROUND

You can use MOXy's external mapping document to override the supertype of IFile to make your use case work (see: 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>

Demo

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:


Another work around could be -

@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;
}
}

Demo-

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);
}

    }


来源:https://stackoverflow.com/questions/17399333/xml-transient-not-working-jaxbmoxy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!