您的位置:首页 > 编程语言 > Java开发

【Spring】Spring Framework Reference Documentation中文版17

2017-05-03 18:04 393 查看

21. Marshalling XML using O/X Mappers

使用O/X映射来整理xml

 

21.1 Introduction

介绍

 

In this chapter, we will describe Spring’s Object/XML Mapping support. Object/XML Mapping, or O/X mapping for short, is the act of converting an XML document to and from an object. This
conversion process is also known as XML Marshalling, or XML Serialization. This chapter uses these terms interchangeably.

在本节中,我们将会描述spring的object/xml映射支持。object/xml映射支持或使用O/X映射作为简写,实际上是将object转化为xml文档。这种转化可以被xml处理知道或xml序列号。这一节会交换的使用这两种方式来说明。

 

Within the field of O/X mapping, a marshaller is responsible for serializing an object (graph) to XML. In similar fashion, an unmarshaller deserializes the XML to an object graph. This XML can take the form of a DOM document, an input or output stream, or
a SAX handler.

关于field的O/X映射,会将一个object(graph)转换为xml。通常,一个编组器并行化一个xml为object的graph。xml可以以dom文档的形式,一个输入或输出流,或一个sax处理器。

 

Some of the benefits of using Spring for your O/X mapping needs are:

一些使用spring关于O/X映射的益处是:

 

21.1.1 Ease of configuration

易于配置

 

Spring’s bean factory makes it easy to configure marshallers, without needing to construct JAXB context, JiBX binding factories, etc. The marshallers can be configured as any other bean
in your application context. Additionally, XML Schema-based configuration is available for a number of marshallers, making the configuration even simpler.

spring的bean工厂使得可以简单的配置编组器,而不需要结构化的JAXB上下文,JiBX绑定工厂等等。编组器可以被配置作为任何其他bean在你的应用上下文中。此外,基于xml
schema配置是对于一系列编组器可行的,使得配置更加简单。

 

21.1.2 Consistent Interfaces

一致的接口

 

Spring’s O/X mapping operates through two global interfaces: the Marshaller and Unmarshaller interface. These abstractions allow you to switch O/X mapping frameworks with relative ease,
with little or no changes required on the classes that do the marshalling. This approach has the additional benefit of making it possible to do XML marshalling with a mix-and-match approach (e.g. some marshalling performed using JAXB, other using XMLBeans)
in a non-intrusive fashion, leveraging the strength of each technology.

spring的O/X映射操作通过两个全局的接口:Marshaller接口和Unmarshaller接口。这个抽象允许你切花O/X映射框架十分的简单化,仅需要一点或不需要改变类实现编组。这种方法有额外的好吃就是可以mix-and-match的方式来实现xml编组(例如,一些编组使用JAXB)在非侵入的形式,可以减少对于技术的依赖。

 

21.1.3 Consistent Exception Hierarchy

一致的异常结构

 

Spring provides a conversion from exceptions from the underlying O/X mapping tool to its own exception hierarchy with the XmlMappingException as the root exception. As can be expected, these runtime exceptions wrap the original exception so no information
is lost.

spring提供了一个方便的从异常转换为底层的O/X映射工具为自己的异常结构当XmlMappingException作为一个根异常。作为考虑,这些运行时异常包裹原始异常因此没有信息会丢失。

 

21.2 Marshaller and Unmarshaller

 

As stated in the introduction, a marshaller serializes an object to XML, and an unmarshaller deserializes XML stream to an object. In this section, we will describe the two Spring interfaces used for this purpose.

就像开始时介绍中说的,编码一个object为xml并且解码一个xml流为object。在这种选择,我们将会描述两个spring的接口用于这个目的。

 

21.2.1 Marshaller

Marshaller

 

Spring abstracts all marshalling operations behind the org.springframework.oxm.Marshaller interface, the main methods of which is listed below.

spring抽象所有的编码操作在org.springframework.oxm.Marshaller接口下,主要的方法列在下面。

 

public interface Marshaller {

 

    /**

     * Marshals the object graph with the given root into the provided Result.

     */

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

 

}

 

The Marshaller interface has one main method, which marshals the given object to a given javax.xml.transform.Result. Result is a tagging interface that basically represents an XML output abstraction: concrete implementations wrap various XML representations,
as indicated in the table below.

Marshaller接口有一个主要的方法,将给定的object转换为javax.xml.transform.Result。Result是一个标记接口基于代表xml输出抽象:具体实施包裹不同xml代表,列在下面的表格中。

 

Result implementation

Result实现

Wraps XML representation

包裹xml处理

DOMResult

org.w3c.dom.Node

SAXResult

org.xml.sax.ContentHandler

StreamResult

java.io.File, java.io.OutputStream, or java.io.Writer

 

[Note]

注意

 

Although the marshal() method accepts a plain object as its first parameter, most Marshaller implementations cannot handle arbitrary objects. Instead, an object class must be mapped in a mapping file, marked with an annotation, registered with the marshaller,
or have a common base class. Refer to the further sections in this chapter to determine how your O/X technology of choice manages this.

尽管marshal方法接收一个普通的object作为他第一个参数,大部分Marshaller实现不能处理任意的object。作为代替,一个object类必须对应一个映射文件,并使用注解标记,使用marshaller来注册或有一个共同的基类。引用这节中较远的内容将决定如何来选择O/X技术来管理这些。

 

21.2.2 Unmarshaller

 

Similar to the Marshaller, there is the org.springframework.oxm.Unmarshaller interface.

和Marshaller相似,是org.springframework.oxm.Unmarshaller接口。

 

public interface Unmarshaller {

 

    /**

     * Unmarshals the given provided Source into an object graph.

     */

    Object unmarshal(Source source) throws XmlMappingException, IOException;

}

 

This interface also has one method, which reads from the given javax.xml.transform.Source (an XML input abstraction), and returns the object read. As with Result, Source is a tagging interface that has three concrete implementations. Each wraps a different
XML representation, as indicated in the table below.

这个接口也有一个方法,可以读取给定的javax.xml.transform.Source(一个xml输入抽象),并返回读取的object。作为结果,源是一个标记接口有三个实现。每个包裹不同的xml,列在下面的表中。

Source implementation

源实现

Wraps XML representation

包裹xml处理

DOMSource

org.w3c.dom.Node

SAXSource

org.xml.sax.InputSource, and org.xml.sax.XMLReader

StreamSource

java.io.File, java.io.InputStream, or java.io.Reader

Even though there are two separate marshalling interfaces ( Marshaller and Unmarshaller), all implementations found in Spring-WS implement both in one class. This means that you can wire up one marshaller class and refer to it both as a marshaller and an
unmarshaller in your applicationContext.xml.

即使有两个分开的marshalling接口(Marshaller和Unmarshaller),所有的实现基于spring-ws实现。这意味着你可以处理一个marshaller类病引用一个marshaller和一个unmarshaller在你的applicationContext.xml中。

 

21.2.3 XmlMappingException

 

Spring converts exceptions from the underlying O/X mapping tool to its own exception hierarchy with the XmlMappingException as the root exception. As can be expected, these runtime exceptions wrap the original exception so no information will be lost.

spring转换异常从底层的O/X映射工具为自己的异常结构使用XmlMappingException作为根异常。根据期待,这些运行时异常包裹原始的异常因此没有信息会丢失。

 

Additionally, the MarshallingFailureException and UnmarshallingFailureException provide a distinction between marshalling and unmarshalling operations, even though the underlying O/X mapping tool does not do so.

此外,MarshallingFailureException和UnmarshallingFailureException提供了一个明显在marshalling和unmarshalling操作的区别,即使在底层O/X映射工具不会这么做。

 

The O/X Mapping exception hierarchy is shown in the following figure:

O/X映射异常结构展示在下面的图中:

 


 

O/X Mapping exception hierarchy

O/X映射异常结构

 

21.3 Using Marshaller and Unmarshaller

使用Marshaller和Unmarshaller

 

Spring’s OXM can be used for a wide variety of situations. In the following example, we will use it to marshal the settings of a Spring-managed application as an XML file. We will use
a simple JavaBean to represent the settings:

spring的OXM可以被广泛使用在不同的情况下。在下面的例子中,我们将使用它来处理设置spring管理的应用作为一个XML文件。我们将使用一个简单JavaBean来代表这些设置:

 

public class Settings {

 

    private boolean fooEnabled;

 

    public boolean isFooEnabled() {

        return fooEnabled;

    }

 

    public void setFooEnabled(boolean fooEnabled) {

        this.fooEnabled = fooEnabled;

    }

}

 

The application class uses this bean to store its settings. Besides a main method, the class has two methods: saveSettings() saves the settings bean to a file named settings.xml, and loadSettings() loads these settings again. A main() method constructs a
Spring application context, and calls these two methods.

应用类使用这些bean来存储他的设置。在主方法下,类有两个方法:saveSettings报错设置bean到一个文件名字为settings.xml,并且loadSettings加载这些设置。一个main方法构造一个spring应用上下文并且调用这两个方法。

 

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import javax.xml.transform.stream.StreamResult;

import javax.xml.transform.stream.StreamSource;

 

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import org.springframework.oxm.Marshaller;

import org.springframework.oxm.Unmarshaller;

 

public class Application {

 

    private static final String FILE_NAME = "settings.xml";

    private Settings settings = new Settings();

    private Marshaller marshaller;

    private Unmarshaller unmarshaller;

 

    public void setMarshaller(Marshaller marshaller) {

        this.marshaller = marshaller;

    }

 

    public void setUnmarshaller(Unmarshaller unmarshaller) {

        this.unmarshaller = unmarshaller;

    }

 

    public void saveSettings() throws IOException {

        FileOutputStream os = null;

        try {

            os = new FileOutputStream(FILE_NAME);

            this.marshaller.marshal(settings, new StreamResult(os));

        } finally {

            if (os != null) {

                os.close();

            }

        }

    }

 

    public void loadSettings() throws IOException {

        FileInputStream is = null;

        try {

            is = new FileInputStream(FILE_NAME);

            this.settings = (Settings) this.unmarshaller.unmarshal(new StreamSource(is));

        } finally {

            if (is != null) {

                is.close();

            }

        }

    }

 

    public static void main(String[] args) throws IOException {

        ApplicationContext appContext =

                new ClassPathXmlApplicationContext("applicationContext.xml");

        Application application = (Application) appContext.getBean("application");

        application.saveSettings();

        application.loadSettings();

    }

}

 

The Application requires both a marshaller and unmarshaller property to be set. We can do so using the following applicationContext.xml:

应用要求设置一个marshaller和unmarshaller属性。我们可以这样做使用下面的applicationContext.xml:

 

<beans>

    <bean id="application" class="Application">

        <property name="marshaller" ref="castorMarshaller" />

        <property name="unmarshaller" ref="castorMarshaller" />

    </bean>

    <bean id="castorMarshaller" class="org.springframework.oxm.castor.CastorMarshaller"/>

</beans>

 

This application context uses Castor, but we could have used any of the other marshaller instances described later in this chapter. Note that Castor does not require any further configuration by default, so the bean definition is rather simple. Also note
that the CastorMarshaller implements both Marshaller and Unmarshaller, so we can refer to the castorMarshaller bean in both the marshaller and unmarshaller property of the application.

应用上下文使用Castor,但是我们可以使用任意一个其他的marshaller实例描述在后面的章节中。注意Castor不要求任何默认配置,因此bean定义是很简单的。注意CastorMarshaller实现了Marshaller和Unmarshaller接口,因此我们可以引用castorMarshaller的bean在应用的marshaller和unmarshaller属性中。

 

This sample application produces the following settings.xml file:

样例的应用产生如下的settings.xml文件:

 

<?xml version="1.0" encoding="UTF-8"?>

<settings foo-enabled="false"/>

 

21.4 XML Schema-based Configuration

基于xml schema的配置

 

Marshallers could be configured more concisely using tags from the OXM namespace. To make these tags available, the appropriate schema has to be referenced first in the preamble of the XML configuration file. Note the 'oxm' related text below:

Marshallers可以被配置多个简明的标签来自OXM的命名空间。为了使用这些标签,适当的scheam可以引用xml配置文件中的序文。注意'oxm'相关的文本如下:

 

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xmlns:oxm="http://www.springframework.org/schema/oxm" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm.xsd">
 

Currently, the following tags are available:

当前,下面的标签是可用:

 

    jaxb2-marshaller

    xmlbeans-marshaller

    castor-marshaller

    jibx-marshaller

 

Each tag will be explained in its respective marshaller’s section. As an example though, here is how the configuration of a JAXB2 marshaller might look like:

每个标签将被解释为相应的marshaller的内容。如下下面的例子,是一个如何配置JAXB2的marshaller的内容:

 

<oxm:jaxb2-marshaller id="marshaller" contextPath="org.springframework.ws.samples.airline.schema"/>

 

21.5 JAXB

 

The JAXB binding compiler translates a W3C XML Schema into one or more Java classes, a jaxb.properties file, and possibly some resource files. JAXB also offers a way to generate a schema from annotated Java classes.

JAXB绑定编译器翻译一个W3C的xml的schema为一个或多个Java类,一个jaxb.properties文件,有一些源文件。JAXB也提供了一种方式生成scheam来自注解Java类。

 

Spring supports the JAXB 2.0 API as XML marshalling strategies, following the Marshaller and Unmarshaller interfaces described in Section 21.2,
“Marshaller and Unmarshaller”. The corresponding integration classes reside in the org.springframework.oxm.jaxb
package.

spring提供了JAXB2.0的API作为xml的marshalling策略,符合章节21.2中描述的Marshaller和Unmarshaller接口,“Marshaller
and Unmarshaller”。相应的集成类在org.springframework.oxm.jaxb包中。

 

21.5.1 Jaxb2Marshaller

 

The Jaxb2Marshaller class implements both the Spring Marshaller and Unmarshaller interface. It requires a context path to operate, which you can set using the contextPath property. The context path is a list of colon (:) separated Java package names that
contain schema derived classes. It also offers a classesToBeBound property, which allows you to set an array of classes to be supported by the marshaller. Schema validation is performed by specifying one or more schema resource to the bean, like so:

Jaxb2Marshaller类实现了spring的Marshaller和Unmarshaller接口。他要求一个上下文路径来操作,你可以设置使用contextPath属性。上下文路径是一个冒号分隔的列表分隔Java包的名字包含schema的衍生类。他也引用了一个classesToBeBound属性,允许你设置一个类的数组来支持通过marshaller。schema的验证表现指定一个或多个schema资源对于bean,例如:

 

<beans>

    <bean id="jaxb2Marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">

        <property name="classesToBeBound">

            <list>

                <value>org.springframework.oxm.jaxb.Flight</value>

                <value>org.springframework.oxm.jaxb.Flights</value>

            </list>

        </property>

        <property name="schema" value="classpath:org/springframework/oxm/schema.xsd"/>

    </bean>

 

    ...

 

</beans>

 

XML Schema-based Configuration

基于xml的scheam的配置

 

The jaxb2-marshaller tag configures a org.springframework.oxm.jaxb.Jaxb2Marshaller. Here is an example:

jaxb2-marshaller标签配置一个org.springframework.oxm.jaxb.Jaxb2Marshaller。这是一个列子:

 

<oxm:jaxb2-marshaller id="marshaller" contextPath="org.springframework.ws.samples.airline.schema"/>

 

Alternatively, the list of classes to bind can be provided to the marshaller via the class-to-be-bound child tag:

作为替代,类的列表绑定可以提供对于marshaller通过class-to-be-bound的子标签:

 

<oxm:jaxb2-marshaller id="marshaller">

    <oxm:class-to-be-bound name="org.springframework.ws.samples.airline.schema.Airport"/>

    <oxm:class-to-be-bound name="org.springframework.ws.samples.airline.schema.Flight"/>

    ...

</oxm:jaxb2-marshaller>

 

Available attributes are:

可用的属性如下:

Attribute

属性

Description

描述

Required

必填

id

the id of the marshaller

no

contextPath

the JAXB Context path

no

 

21.6 Castor

 

Castor XML mapping is an open source XML binding framework. It allows you to transform the data contained in a java object model into/from an XML document. By default, it does not require any further configuration, though a mapping file can be used to have
more control over the behavior of Castor.

Castor的xml映射是一个开源的xml绑定框架。允许你转换数据在java的object模型和一个xml文档之间。默认他不要求任何配置,通过要求更多的配置,通过一个映射文件可以被使用多个控制在Castor的行为中。

 

For more information on Castor, refer to the Castor web site. The Spring integration classes reside in the org.springframework.oxm.castor package.

对于Casotr的信息,引用Castor的web站点。spring集成类在org.springframework.oxm.castor包中。

 

21.6.1 CastorMarshaller

 

As with JAXB, the CastorMarshaller implements both the Marshaller and Unmarshaller interface. It can be wired up as follows:

使用JAXB,CastorMarshaller实现了Marshaller和Unmarshaller接口。可以被处理如下:

 

<beans>

    <bean id="castorMarshaller" class="org.springframework.oxm.castor.CastorMarshaller" />

    ...

</beans>

 

21.6.2 Mapping

映射

 

Although it is possible to rely on Castor’s default marshalling behavior, it might be necessary to have more control over it. This can be accomplished using a Castor mapping file. For
more information, refer to Castor XML Mapping.

尽管可以依赖Castor的默认marshalling行为,他需要有更多的控制。可以使用Castor的映射文件来实现。更多的信息,请参考Castor的xml映射。

 

The mapping can be set using the mappingLocation resource property, indicated below with a classpath resource.

映射可以使用mappingLocation的资源属性,指示一个类路径资源。

 

<beans>

    <bean id="castorMarshaller" class="org.springframework.oxm.castor.CastorMarshaller" >

        <property name="mappingLocation" value="classpath:mapping.xml" />

    </bean>

</beans>

 

XML Schema-based Configuration

基于xml的schema的配置

 

The castor-marshaller tag configures a org.springframework.oxm.castor.CastorMarshaller. Here is an example:

castor-marshaller标签配置了一个org.springframework.oxm.castor.CastorMarshaller。下面是给定的例子:

 

<oxm:castor-marshaller id="marshaller" mapping-location="classpath:org/springframework/oxm/castor/mapping.xml"/>

 

The marshaller instance can be configured in two ways, by specifying either the location of a mapping file (through the mapping-location property), or by identifying Java POJOs (through the target-class or target-package properties) for which there exist
corresponding XML descriptor classes. The latter way is usually used in conjunction with XML code generation from XML schemas.

marshaller实例可以配置以两种方式,通过指定映射文件的位置(通过mapping-location属性),或通过定义Java POJOs(通过target-class或target-package属性)存在于相应的xml描述类中。最后的方式通过使用配合xml代码的生成来自xml的scheam。

 

Available attributes are:

可用的属性如下:

Attribute

属性

Description

描述

Required

是否必须

id

the id of the marshaller

no

encoding

the encoding to use for unmarshalling from XML

处理的xml文件的编码格式

no

target-class

a Java class name for a POJO for which an XML class descriptor is available (as generated through code generation)

一个Java类名对于一个POJO或一个xml类描述符(通过代码生成)

no

target-package

a Java package name that identifies a package that contains POJOs and their corresponding Castor XML descriptor classes (as generated through code generation from XML schemas)

一个Java包名指定包中所含有的POJO和他们相应的Casotr的xml描述符类(可以通过xml的schema来生成)

no

mapping-location

location of a Castor XML mapping file

Castor的xml映射文件的位置

no

 

21.7 XMLBeans

 

XMLBeans is an XML binding tool that has full XML Schema support, and offers full XML Infoset fidelity. It takes a different approach to that of most other O/X mapping frameworks, in that all classes that are generated from an XML Schema are all derived
from XmlObject, and contain XML binding information in them.

XMLBeans是一个xml绑定工具有全面的xml的schema的支持,并且提供全面的xml信息集合。有不同的方式对于大部分其他的O/X映射框架,在所以的类生产来自xml的schema是来源于XmlObject,并且包含xml绑定信息在其中。

 

For more information on XMLBeans, refer to the XMLBeans web site . The Spring-WS integration classes reside in the org.springframework.oxm.xmlbeans package.

对于XMLBeans的更多信息,引用XMLBeans的web站点。spring-ws集成类在org.springframework.oxm.xmlbeans包中。

 

21.7.1 XmlBeansMarshaller

 

The XmlBeansMarshaller implements both the Marshaller and Unmarshaller interfaces. It can be configured as follows:

XmlBeansMarshaller实现了Marshaller和Unmarshaller接口。可以配置如下:

 

<beans>

 

    <bean id="xmlBeansMarshaller" class="org.springframework.oxm.xmlbeans.XmlBeansMarshaller" />

    ...

 

</beans>

 

[Note]

注意

 

Note that the XmlBeansMarshaller can only marshal objects of type XmlObject, and not every java.lang.Object.

注意XmlBeansMarshaller只能整理XmlObject类型的object,并且不是所有的java.lang.Object。

 

XML Schema-based Configuration

基于xml的schema的配置

 

The xmlbeans-marshaller tag configures a org.springframework.oxm.xmlbeans.XmlBeansMarshaller. Here is an example:

xmlbeans-marshaller标签配置了一个org.springframework.oxm.xmlbeans.XmlBeansMarshaller。例如:

 

<oxm:xmlbeans-marshaller id="marshaller"/>

 

Available attributes are:

可用的属性是:

Attribute

属性

Description

描述

Required

是否必须

id

the id of the marshaller

no

options

the bean name of the XmlOptions that is to be used for this marshaller. Typically a
XmlOptionsFactoryBean definition

XmlOptions的bean的名字可以被使用对于marshaller。通常是一个XmlOptionsFactoryBean的定义

no

 

21.8 JiBX

 

The JiBX framework offers a solution similar to that which JDO provides for ORM: a binding definition defines the rules for how your Java objects are converted to or from XML. After preparing the binding and compiling the classes, a JiBX binding compiler
enhances the class files, and adds code to handle converting instances of the classes from or to XML.

JiBX框架提供了一个解决方案对于JDO提供为了ORM:一个绑定的定义定义了规则关于你的Java的object如何转换从xml中。在准备绑定和编译类之后,一个JiBX绑定编译增强类文件,并且添加代码来处理转换类和xml之间。

 

For more information on JiBX, refer to the JiBX web site. The Spring integration classes reside in the org.springframework.oxm.jibx package.

关于更多JiBX的信息,参考JiBX的web站点。spring集成类在org.springframework.oxm.jibx包中。

 

21.8.1 JibxMarshaller

 

The JibxMarshaller class implements both the Marshaller and Unmarshaller interface. To operate, it requires the name of the class to marshal in, which you can set using the targetClass property. Optionally, you can set the binding name using the bindingName
property. In the next sample, we bind the Flights class:

JibxMarshaller类实现了Marshaller和Unmarshaller接口。为了操作,他要求类名和你可以设置使用targetClass属性。可选的,你可以设置绑定的名字使用bindingName属性。下面的例子,我们绑定了Flights类:

 

<beans>

    <bean id="jibxFlightsMarshaller" class="org.springframework.oxm.jibx.JibxMarshaller">

        <property name="targetClass">org.springframework.oxm.jibx.Flights</property>

    </bean>

    ...

</beans>

 

A JibxMarshaller is configured for a single class. If you want to marshal multiple classes, you have to configure multiple JibxMarshallers with different targetClass property values.

JibxMarshaller被配置对于单个类。如果你希望对于多个类,你可以配置多个JibxMarshallers实用不同的targetClass属性值。

 

XML Schema-based Configuration

基于xml的schema的配置

 

The jibx-marshaller tag configures a org.springframework.oxm.jibx.JibxMarshaller. Here is an example:

jibx-marshaller标签配置了一个org.springframework.oxm.jibx.JibxMarshaller。下面是一个例子:

 

<oxm:jibx-marshaller id="marshaller" target-class="org.springframework.ws.samples.airline.schema.Flight"/>

 

Available attributes are:

可用的属性如下:

Attribute

属性

Description

描述

Required

是否必须

id

the id of the marshaller

no

target-class

the target class for this marshaller

这个marshaller的目标类

yes

bindingName

the binding name used by this marshaller

这个marshaller使用的绑定名

no

 

21.9 XStream

 

XStream is a simple library to serialize objects to XML and back again. It does not require any mapping, and generates clean XML.

XStream是一个简单的库来序列化object为xml并转换回object。不需要任何映射并且生成干净的xml文件。

 

For more information on XStream, refer to the XStream web site. The Spring integration classes reside in the org.springframework.oxm.xstream package.

关于XStream的更多信息,参考XStream的wenb站点。spring集成类在org.springframework.oxm.xstream包中。

 

21.9.1 XStreamMarshaller

 

The XStreamMarshaller does not require any configuration, and can be configured in an application context directly. To further customize the XML, you can set analias map, which consists of string aliases mapped to classes:

XStreamMarshaller不需要任何配置,并且可以直接配置在应用上下文中。对于自定义xml,你可以设置analias的映射,包含字符串对于类的别名:

 

<beans>

    <bean id="xstreamMarshaller" class="org.springframework.oxm.xstream.XStreamMarshaller">

        <property name="aliases">

            <props>

                <prop key="Flight">org.springframework.oxm.xstream.Flight</prop>

            </props>

        </property>

    </bean>

    ...

</beans>

 

[Warning]

警告

 

By default, XStream allows for arbitrary classes to be unmarshalled, which can result in security vulnerabilities. As such, it is not recommended to use the XStreamMarshaller to unmarshal XML from external sources (i.e. the Web), as this can result in security
vulnerabilities. If you do use the XStreamMarshaller to unmarshal XML from an external source, set the supportedClasses property on the XStreamMarshaller, like so:

默认,XStream允许任意类被unmarshalled,意味着在安全范围内。例如,不建议使用XStreamMarshaller在unmarshal的xml来自外部资源(例如,web)作为可能的安全漏洞范围。如果你使用XStreamMarshaller对于unmarshal的xml来自外部的资源,设置XStreamMarshaller的supportedClasses属性,如下:

 

<bean id="xstreamMarshaller" class="org.springframework.oxm.xstream.XStreamMarshaller">

    <property name="supportedClasses" value="org.springframework.oxm.xstream.Flight"/>

    ...

</bean>

 

This will make sure that only the registered classes are eligible for unmarshalling.

这将保证只有注册类可以有资格用于unmarshalling。

 

Additionally, you can register custom converters to make sure that only your supported classes can be unmarshalled. You might want to add a CatchAllConverter as the last converter in the list, in addition to converters that explicitly support the domain
classes that should be supported. As a result, default XStream converters with lower priorities and possible security vulnerabilities do not get invoked.

此外,你可以注册自定义的转化器来保证只有你支持的类可以被unmarshalled。你可能希望添加一个CatchAllConverter作为最后一个转化器在列表中,此外转换器明显支持应当支持的实体类。因此,默认的XStream的转化器有低优先级并且可以没有安全漏洞的调用。

 

[Note]

注意

 

Note that XStream is an XML serialization library, not a data binding library. Therefore, it has limited namespace support. As such, it is rather unsuitable for usage within Web services.

注意XStream是一个xml的序列号库,不是一个数据绑定库。因此,他没有命名空间的支持限制。因此,他不适合使用于web服务中。

 

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: