Java 如何使用XML/XSD定义JAXB抽象类并实现继承?
正在寻找一种方法来定义一个基础XML模式,将其作为XML消息重新用于其他模式,这些消息将用于不同的JMS消息有效负载。目的是:Java 如何使用XML/XSD定义JAXB抽象类并实现继承?,java,xml,jaxb,xsd,jms,Java,Xml,Jaxb,Xsd,Jms,正在寻找一种方法来定义一个基础XML模式,将其作为XML消息重新用于其他模式,这些消息将用于不同的JMS消息有效负载。目的是: XmlMessage—定义一个基本消息类,所有消息都将作为该类发送 Header—所有消息将包含有关消息的元数据的类 Body—每个特定消息实现可用于定义每个消息的messagee Payload的抽象类 这种方法将: 允许JMS EJB将传入的XML解组到高级通用XmlMessage中,然后能够评估主体实现的类类型以确定如何处理该消息 标准化各种消息格式 利用J
- XmlMessage—定义一个基本消息类,所有消息都将作为该类发送
- Header—所有消息将包含有关消息的元数据的类
- Body—每个特定消息实现可用于定义每个消息的messagee Payload的抽象类
- 允许JMS EJB将传入的XML解组到高级通用XmlMessage中,然后能够评估主体实现的类类型以确定如何处理该消息
- 标准化各种消息格式
- 利用JAXB生成Java类,用于发送消息的客户端和处理消息的EJB
- 利用JAXB在消息负载中封送/解封送XML
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://www.samnunnally.com" targetNamespace="http://www.samnunnally.com" version="1.0">
<xs:element name="xmlMessage">
<xs:complexType>
<xs:sequence>
<xs:element name="header" type="header" minOccurs="1" maxOccurs="1"/>
<xs:element ref="body"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="header">
<xs:sequence>
<xs:element name="message_class" type="xs:string"/>
<xs:element name="message_id" type="xs:int"/>
<xs:element name="message_length" type="xs:int"/>
<xs:element name="software_version" type="xs:string"/>
<xs:element name="correlation_id" type="xs:string"/>
<xs:element name="session_id" type="xs:string"/>
<xs:element name="return_code" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:element name="body" type="body"/>
<xs:complexType name="body" abstract="true">
<xs:sequence/>
</xs:complexType>
Foo消息:
package com.samnunnally;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "foo", propOrder = {
"customers"
})
public class Foo
extends Body
{
@XmlElement(required = true)
protected List<Customer> customers;
public List<Customer> getCustomers() {
if (customers == null) {
customers = new ArrayList<Customer>();
}
return this.customers;
}
}
package com.samnunally;
导入java.util.ArrayList;
导入java.util.List;
导入javax.xml.bind.annotation.XmlAccessType;
导入javax.xml.bind.annotation.XmlAccessorType;
导入javax.xml.bind.annotation.xmlement;
导入javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name=“foo”,proporter={
“客户”
})
公开课Foo
伸展身体
{
@XmlElement(必需=true)
受保护名单客户;
公共列表getCustomers(){
如果(客户==null){
客户=新的ArrayList();
}
把这个还给顾客;
}
}
package com.samnunnally;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementRef;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"header",
"body"
})
@XmlRootElement(name = "xmlMessage")
public class XmlMessage {
@XmlElement(required = true)
protected Header header;
@XmlElementRef(name = "body", namespace = "http://www.samnunnally.com", type = JAXBElement.class)
protected JAXBElement<? extends Body> body;
public Header getHeader() {
return header;
}
public void setHeader(Header value) {
this.header = value;
}
public JAXBElement<? extends Body> getBody() {
return body;
}
public void setBody(JAXBElement<? extends Body> value) {
this.body = value;
}
}
package com.samnunnally;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlSeeAlso;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "body")
@XmlSeeAlso({
Foo.class
})
public abstract class Body {
}
package com.samnunnally;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "foo", propOrder = {
"customers"
})
public class Foo
extends Body
{
@XmlElement(required = true)
protected List<Customer> customers;
public List<Customer> getCustomers() {
if (customers == null) {
customers = new ArrayList<Customer>();
}
return this.customers;
}
}