jaxb - @XmlDiscriminatorNode/@XmlDecriminatorValue 不适用于 WebLogic Server

标签 jaxb eclipselink weblogic-10.x moxy

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

我正在使用 EclipseLink MOXy 2.4.1 和 WebLogic 10.3.2 并且在 WebLogic 中配置了 MOXy 2.4.1

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

}

子类

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.properties

也因为 @XmlDescriminatorNode/@XmlDescriminatorValue是需要确保将 MOXy 指定为 JAXB 提供程序的 MOXy 扩展。这是通过添加一个名为 jaxb.properties 的文件来完成的。在与域模型相同的包中包含以下条目。
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory

演示

下面是一个模拟 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;
        }
    }

}

输出

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

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

更多信息
  • Specifying EclipseLink MOXy as Your JAXB Provider
  • JAXB and Inheritance - MOXy Extension @XmlDescriminatorNode/@XmlDescrimintatorValue
  • Updating EclipseLink in WebLogic
  • 关于jaxb - @XmlDiscriminatorNode/@XmlDecriminatorValue 不适用于 WebLogic Server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13831189/

    相关文章:

    java - JaxB xjc 生成的 pojos 包含空白命名空间

    java - 使用 JAXB 忽略根元素

    jpa - 是否可以使用EclipseLink输出生成的SQL,而不必增加日志的详细程度?

    java - weblogic 中的热修复

    deployment - 如何在weblogic中远程部署应用程序?

    java - 开源线程池库

    java - JAXB 解码返回 null

    xml - 使用 JAXB 需要 JSON 字段

    java - 具有 JPA、PostgreSQL 和 NULL 值的 JodaTime

    spring - 在 EclipseLink MOXy 中使用多重继承