以下是我用于在 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;
}
}
子类
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,则无法创建子类。