问题描述
以下是我在WebLogic 10.3.2版本上使用MOXy JAXB转换创建子类时使用的类。我使用EclipseLink 2.4.1 MOXy来生成XML。我无法在以下代码中生成type属性。如果我在这里做错了,请告诉我。
Following are the classes I am using for create sub classes using MOXy JAXB conversion on WebLogic 10.3.2 version. I am using the EclipseLink 2.4.1 MOXy for generating the XML. I am unable to generate the type attribute in the following code. Let me know if I am doing anything wrong here.
我正在使用EclipseLink MOXy 2.4.1和WebLogic 10.3.2以及在WebLogic中配置MOXy 2.4.1 / p>
I am using EclipseLink MOXy 2.4.1 and WebLogic 10.3.2 and MOXy 2.4.1 is configured in the 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。
The above is generating the following XML.
<representation>
<firstName>first-name</firstName>
<lastName>last-name</lastName>
</representation>
上面没有生成属性类型。
The attribute type is not generated in the above.
非常感谢。是的,我错过了上面的jaxb.properties。
另外,当我使用PUT或POST时,当反序列化XML时,如果不存在@XmlSeeAlso,则无法创建子类。
Thanks a lot. Yes, I missed jaxb.properties in the above.Also, Yes when I use the PUT or POST, when XML is de-serialized, it is not able to create the subclasses if @XmlSeeAlso is not present.
推荐答案
有几项可能会导致您出现问题。
There are a couple items that may be causing you problems.
BaseEntity
默认情况下,JAX-RS实现会在...上创建 JAXBContext
返回服务方法的类型或参数,在本例中为 Represenatation
。在处理域模型时,JAXB impl还将引入引用类型,例如 BaseEntity
。它不能自动拉入子类,因此我们可以使用 @XmlSeeAlso
注释来引用它们。
By default a JAX-RS implementation creates a JAXBContext
on the return type or parameter of the service method, in this case Represenatation
. When processing the domain model the JAXB impl will also pull in referred types such as BaseEntity
. It can't automatically pull in subclasses so we can use the @XmlSeeAlso
annotation to reference those.
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扩展,您需要确保将MOXy指定为JAXB提供程序。这是通过在与您的域模型相同的包中添加名为 jaxb.properties
的文件来完成的,其中包含以下条目。
Also since @XmlDescriminatorNode
/@XmlDescriminatorValue
are MOXy extensions you need to make sure you specify MOXy as your JAXB provider. This is done by adding a file named jaxb.properties
in the same package as your domain model with the following entry.
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
演示
以下是一个模仿RESTful服务功能的独立示例。
Below is a standalone example that mimics what your RESTful service does.
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;
}
}
}
输出
以下是运行演示代码的输出。看到类型
属性现在存在。
Below is the output from running the demo code. See that the type
attribute is now present.
<?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
这篇关于@ XmlDiscriminatorNode / @ XmlDescriminatorValue无法在WebLogic Server上运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!