【发布时间】:2017-01-11 04:38:57
【问题描述】:
我正在将 Jaxb2Marshaller 与 Spring 集成一起使用。我有一个入站网关 Web 服务,当有人调用它时,它会自动解析为 JAXB 生成的类。
但是当我调试源代码时,我看到 Jaxb2Marshaller 使用 DOM。我认为它会使用 SAX 将 XML 数据绑定到 Java 对象中,SAX 更快。为什么Jaxb2Marshaller 默认使用 DOM?如何配置它以使用 SAX?
当我检查文档时
unmarshaller 需要一个 Source 实例。如果消息负载不是 Source 的实例, 将尝试转换。目前 String、File 和 org.w3c.dom.Document 有效载荷是 支持的。通过注入一个实现也支持到源的自定义转换 源工厂。 笔记 如果没有明确设置 SourceFactory,则 UnmarshallingTransformer 上的属性将 默认设置为 DomSourceFactory
关于源工厂
我们可以看到,目前只有DomSourceFactory和StringSourceFactory。没有SaxSourceFactory。
所以我们不能将 SAX 与 Jaxb2Marshaller 一起使用,对吧?
以后会有SaxSourceFactory吗?还是从不?
奇怪的是,当我检查 Jaxb2Marshaller 时,我看到代码已经处理了 SAX
XMLReader xmlReader = null;
InputSource inputSource = null;
if (source instanceof SAXSource) {
SAXSource saxSource = (SAXSource) source;
xmlReader = saxSource.getXMLReader();
inputSource = saxSource.getInputSource();
}
else if (source instanceof StreamSource) {
StreamSource streamSource = (StreamSource) source;
if (streamSource.getInputStream() != null) {
inputSource = new InputSource(streamSource.getInputStream());
}
else if (streamSource.getReader() != null) {
inputSource = new InputSource(streamSource.getReader());
}
else {
inputSource = new InputSource(streamSource.getSystemId());
}
}
那么,最后一个问题是我可以配置使用 Spring Integration Web Service 和 JAXB 和 SAX 吗?我错过了什么吗?
这是我的配置:
<ws:inbound-gateway id="inbound-gateway" request-channel="RequestChannel" reply-channel="ResponseChannel"
marshaller="marshaller" unmarshaller="marshaller" />
<int:channel id="RequestChannel" />
<int:channel id="ResponseChannel" />
<bean id="marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="contextPath" value="com.example.webservice.api"/>
</bean>
谢谢你和最好的问候,
芽原
【问题讨论】:
-
@lexicore 你能帮我吗:-)
-
sax 并不快...sax 通常较慢,因为您需要自定义实现所有额外的应用程序逻辑...
-
看来你有什么误解。你显示
<ws:inbound-gateway>,但谈论UnmarshallingTransformer。第一个完全委托给 Spring WSMarshallingUtils.unmarshal() -
@Artem Bilan,我的意思是在
中,我们可以配置解组器,但我们无法配置解组器将使用哪种技术(SAX 或 DOM 或 StAX)。文档说我们可以注入一个 SourceFactory,Spring Integration 会使用它,但是还没有 SaxSourceFactory(只有 DomSourceFactory、StringSourceFactory)。那么我们如何强制 JAXB Unmarshaller 使用 SAX 呢? -
您确定该文档是关于 WS 的吗?当您将
SourceFactory注入<int-ws:inbound-gateway>时,请给我看一个样本。为什么我的回答对你来说还不够?
标签: jaxb spring-integration sax spring-ws