如何使用XML/XSD定义JAXB抽象类并实现继承? [英] How do you use XML/XSD to define JAXB abstract class and implement inheritance?

查看:146
本文介绍了如何使用XML/XSD定义JAXB抽象类并实现继承?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

寻找一种定义基本XML模式的方法,该模式将在其他模式上重用为XML消息,这些XML消息将用于不同的JMS消息有效负载.目的是:

Looking for a way to define a base XML schema to be re-used on additional schemas as XML messages that will be used for different JMS message payloads. The intent is to:

  • XmlMessage-定义一个基本消息类,所有消息将作为该类发送.
  • Header-所有消息将具有包含有关该消息的元数据的类
  • Body-抽象类,每个特定的消息实现可用于定义每个单独消息的messagee payolad

此方法将:

  • 允许JMS EJB将传入的XML解组到高级通用XmlMessage中,然后能够评估Body实现的类类型,以确定如何处理消息.
  • 标准化各种消息格式.
  • 利用JAXB生成将在发送消息的客户端和处理消息的EJB上使用的Java类.
  • 利用JAXB封送/取消封送消息有效负载中的XML.

推荐答案

请参阅以下XSD架构和JAXB实现以完成为消息正文定义抽象类

Please see the following XSD schemas and JAXB implementations to accomplish defining an abstract class for the Message’s Body

XmlMessage.xsd定义所有消息的格式:

XmlMessage.xsd to define the format of all messages:

<?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>

实现XmlMessage.xsd的示例xsd.最终成为包含客户列表的消息Foo.关键是使用替代组属性和xs:extension base ="body"

Example xsd implementing XmlMessage.xsd. Ends up being message Foo containing a list of customers. The key is to use substitutionGroup attribute and xs:extension base="body"

<?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:include schemaLocation="XmlMessage.xsd"/>
<xs:element name="foo" type="foo" substitutionGroup="body"/>
<xs:complexType name="foo">
    <xs:complexContent>
        <xs:extension base="body">
            <xs:sequence>
                <xs:element name="customers" type="customer" minOccurs="1" maxOccurs="unbounded"/>
            </xs:sequence>
        </xs:extension>
    </xs:complexContent>
</xs:complexType>
<xs:complexType name="customer">
    <xs:sequence>
        <xs:element name="customer_id" type="customerId" nillable="false" minOccurs="1" maxOccurs="1"/>
        <xs:element name="name" type="xs:string"/>
        <xs:element name="street" type="xs:string"/>
        <xs:element name="city" type="xs:string"/>
        <xs:element name="state" type="xs:string"/>
        <xs:element name="zip" type="xs:string"/>
        <xs:element name="phone" type="xs:string"/>
    </xs:sequence>
</xs:complexType>
<xs:complexType name="customerId">
    <xs:sequence>
        <xs:element name="id" type="xs:string" minOccurs="1" maxOccurs="1"/>
    </xs:sequence>
</xs:complexType>

JAXB生成的关键Java类:

Key Java classes generated by JAXB:

消息:

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

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

}

这篇关于如何使用XML/XSD定义JAXB抽象类并实现继承?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆