加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Part V. 数据访问-21. 使用O/X Mappers编组XML-21.2 Marshaller

发布时间:2020-12-16 08:15:55 所属栏目:百科 来源:网络整理
导读:如引言中所述,编组者将对象序列化为XML,解组器将XML流反序列化为对象。 在本节中,我们将介绍用于此目的的两个Spring接口。 21.2.1 Marshaller Spring提取了 org.springframework.oxm.Marshaller 接口后面的所有编组操作,其主要方法如下所示。 public int

如引言中所述,编组者将对象序列化为XML,解组器将XML流反序列化为对象。 在本节中,我们将介绍用于此目的的两个Spring接口。

21.2.1 Marshaller

Spring提取了org.springframework.oxm.Marshaller接口后面的所有编组操作,其主要方法如下所示。

public interface Marshaller {

/**
* Marshal the object graph with the given root into the provided Result.
*/

void marshal(Object graph,Result result) throws XmlMappingException,IOException;
}

Marshaller接口有一个主要方法,它将给定的对象编组给给定的javax.xml.transform.Result。 结果是一个标签接口,基本上表示XML输出抽象:具体实现包装各种XML表示,如下表所示。

Result implementation Wraps XML representation
DOMResult org.w3c.dom.Node
SAXResult org.xml.sax.ContentHandler
StreamResult java.io.File,java.io.OutputStream,orjava.io.Writer

尽管marshal()方法接受一个普通对象作为其第一个参数,但大多数Marshaller实现都不能处理任意对象。 相反,一个对象类必须映射到一个映射文件中,该文件标有注解,在编组器中注册,或者有一个公共的基类。 请参阅本章的其他部分,以确定您选择的O / X技术如何管理此。

21.2.2 Unmarshaller

类似于Marshaller,有org.springframework.oxm.Unmarshaller接口。

Unmarshaller {

/**
* Unmarshal the given provided Source into an object graph.
*/

Object unmarshal(Source source) }

该接口还有一种方法,它从给定的javax.xml.transform.Source(一个XML输入抽象)中读取,并返回读取的对象。 与Result一样,Source是一个具有三个具体实现的标记接口。 每个包装一个不同的XML表示,如下表所示。

Source implementation DOMSource SAXSource org.xml.sax.InputSource,and org.xml.sax.XMLReader
StreamSource java.io.File,java.io.InputStream,or java.io.Reader

即使有两个单独的编组接口(MarshallerUnmarshaller),Spring-WS中的所有实现都在一个类中实现。 这意味着您可以连接一个编组器类,并将其作为应用程序中的编组器和解组器引用。

21.2.3 XmlMappingException

使用XmlMappingException作为根异常,Spring将基础O/X映射工具中的异常转换为其自己的异常层次结构。 可以预期,这些运行时异常包装原始异常,因此不会丢失任何信息。

另外,MarshallingFailureExceptionUnmarshallingFailureException提供编组和解组合操作之间的区别,即使底层O / X映射工具不这样做。

O/X映射异常层次结构如下图所示:


O/X映射异常层次结构

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读