@XmlDiscriminatorNode/@XmlDecriminatorValue 在 WebLogic Server 上不起作用

2024-05-02

以下是我用于在 WebLogic 10.3.2 版本上使用 MOXy JAXB 转换创建子类的类。我使用 EclipseLink 2.4.1 MOXy 来生成 XML。我无法在以下代码中生成类型属性。如果我在这里做错了什么,请告诉我。

我正在使用 EclipseLink MOXy 2.4.1 和 WebLogic 10.3.2,MOXy 2.4.1 是在 WebLogic 中配置的

import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlDiscriminatorNode;

@XmlAccessorType(XmlAccessType.PROPERTY)
@XmlDiscriminatorNode("@type")
public abstract class BaseEntity {

    private String firstName;
    private String lastName;

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

}

Subclass

package forum13831189;

import org.eclipse.persistence.oxm.annotations.XmlDiscriminatorValue;

@XmlDiscriminatorValue("xyz")
public class XyzEntity extends BaseEntity {

    public XyzEntity() {
        super();
    }

}

另一个子类

import org.eclipse.persistence.oxm.annotations.XmlDiscriminatorValue;

@XmlDiscriminatorValue("Abc")
public class AbcEntity extends BaseEntity {
}

RESTful Web 服务类:

@GET
@Path("/xyz")
@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
public Representation getAccount() throws CPAException {
    Representation rep = new Representation();
    BaseEntity entity = new XyzEntity();
    entity.setFirstName("first-name");
    entity.setLastName("last-name");
    rep.setEntity(entity);
    return rep;
}

@XmlRootElement
static class Representation {
    private BaseEntity entity;

    public BaseEntity getEntity() {
        return entity;
    }

    public void setEntity(BaseEntity entity) {
        this.entity = entity;
    }
}

上面生成了以下 XML。

<representation>
     <firstName>first-name</firstName>
      <lastName>last-name</lastName>
 </representation>

上面没有生成属性类型。


多谢。是的,我错过了上面的 jaxb.properties。 另外,是的,当我使用 PUT 或 POST 时,当 XML 被反序列化时,如果 @XmlSeeAlso 不存在,则无法创建子类。


有几个项目可能会给您带来问题。

基础实体

默认情况下,JAX-RS 实现会创建一个JAXBContext在本例中,关于服务方法的返回类型或参数Represenatation。在处理域模型时,JAXB impl 还将引入引用的类型,例如BaseEntity。它不能自动拉入子类,所以我们可以使用@XmlSeeAlso注释来引用那些。

import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlDiscriminatorNode;

@XmlAccessorType(XmlAccessType.PROPERTY)
@XmlDiscriminatorNode("@type")
@XmlSeeAlso({AbcEntity.class, XyzEntity.class})
public abstract class BaseEntity {

    private String firstName;
    private String lastName;

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

}

jaxb.属性

也因为@XmlDescriminatorNode/@XmlDescriminatorValue您需要确保指定 MOXy 作为您的 JAXB 提供者的 MOXy 扩展。这是通过添加一个名为的文件来完成的jaxb.properties在与您的域模型相同的包中,包含以下条目。

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

Demo

下面是一个模仿 RESTful 服务功能的独立示例。

import javax.xml.bind.*;
import javax.xml.bind.annotation.XmlRootElement;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Representation.class);

        Representation rep = new Representation();
        BaseEntity entity = new XyzEntity();
        entity.setFirstName("first-name");
        entity.setLastName("last-name");
        rep.setEntity(entity);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(rep, System.out);
    }

    @XmlRootElement
    static class Representation {
        private BaseEntity entity;

        public BaseEntity getEntity() {
            return entity;
        }

        public void setEntity(BaseEntity entity) {
            this.entity = entity;
        }
    }

}

Output

以下是运行演示代码的输出。看到type属性现已存在。

<?xml version="1.0" encoding="UTF-8"?>
<representation>
   <entity type="xyz">
      <firstName>first-name</firstName>
      <lastName>last-name</lastName>
   </entity>
</representation>

了解更多信息

  • 指定 EclipseLink MOXy 作为您的 JAXB 提供程序 http://blog.bdoughan.com/2011/05/specifying-eclipselink-moxy-as-your.html
  • JAXB 和继承 - MOXy 扩展 @XmlDecriminatorNode/@XmlDecrimintatorValue http://blog.bdoughan.com/2010/11/jaxb-and-inheritance-moxy-extension.html
  • 更新 WebLogic 中的 EclipseLink http://blog.bdoughan.com/2012/10/updating-eclipselink-in-weblogic.html
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

@XmlDiscriminatorNode/@XmlDecriminatorValue 在 WebLogic Server 上不起作用 的相关文章

随机推荐