【发布时间】:2016-04-04 14:11:27
【问题描述】:
阅读 Jersey 源代码后,我注意到 one of the default MessageBodyReader implementations 创建了一个 SAXSource,然后将其传递给 Unmarshaller。
@Override
protected Object readFrom(Class<Object> type, MediaType mediaType,
Unmarshaller u, InputStream entityStream)
throws JAXBException {
final SAXSource s = getSAXSource(spf.provide(), entityStream);
if (type.isAnnotationPresent(XmlRootElement.class)) {
return u.unmarshal(s);
} else {
return u.unmarshal(s, type).getValue();
}
}
我的问题:为什么InputStream 包裹在SAXSource 中?
Unmarshaller.unmarshal 可以自己接受InputStream,com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl 似乎使用相同的内部方法来解组是否提供了SAXSource 或InputStream。唯一的区别似乎是XmlRootElementJaxbProvider 使用依赖注入来提供一个Factory<SAXParserFactory>,该Factory<SAXParserFactory> 用于获取SAXParserFactory,而在UnmarshallerImpl 的内部,方法SAXParserFactory.newInstance() 被直接调用。是否希望能够注入自定义 SAXParserFactory 是使用 unmarshaller.unmarshal(SAXSource) 而不是 unmarshaller.unmarshal(InputStream) 的唯一原因?
【问题讨论】:
标签: java jaxb jersey jersey-2.0