类 Jaxb2Marshaller
- java.lang.Object
- org.springframework.oxm.jaxb.Jaxb2Marshaller
- 所有已实现的接口:
Aware
,BeanClassLoaderAware
,InitializingBean
,GenericMarshaller
,GenericUnmarshaller
,Marshaller
,MimeMarshaller
,MimeUnmarshaller
,Unmarshaller
public class Jaxb2Marshaller extends Object implements MimeMarshaller, MimeUnmarshaller, GenericMarshaller, GenericUnmarshaller, BeanClassLoaderAware, InitializingBean
Implementation of theGenericMarshaller
interface for JAXB 2.1/2.2, as included in JDK 6 update 4+ and Java 7/8.The typical usage will be to set either the "contextPath" or the "classesToBeBound" property on this bean, possibly customize the marshaller and unmarshaller by setting properties, schemas, adapters, and listeners, and to refer to it.
- 从以下版本开始:
- 3.0
- 作者:
- Arjen Poutsma, Juergen Hoeller, Rossen Stoyanchev
- 另请参阅:
setContextPath(java.lang.String)
,setClassesToBeBound(java.lang.Class<?>...)
,setJaxbContextProperties(java.util.Map<java.lang.String, ?>)
,setMarshallerProperties(java.util.Map<java.lang.String, ?>)
,setUnmarshallerProperties(java.util.Map<java.lang.String, ?>)
,setSchema(org.springframework.core.io.Resource)
,setSchemas(org.springframework.core.io.Resource...)
,setMarshallerListener(Marshaller.Listener)
,setUnmarshallerListener(Unmarshaller.Listener)
,setAdapters(<any>...)
构造器概要
构造器 构造器 说明 Jaxb2Marshaller()
方法概要
所有方法 实例方法 具体方法 修饰符和类型 方法 说明 void
afterPropertiesSet()
Invoked by the containingBeanFactory
after it has set all bean properties and satisfiedBeanFactoryAware
,ApplicationContextAware
etc.protected XmlMappingException
convertJaxbException(JAXBException ex)
Convert the givenJAXBException
to an appropriate exception from theorg.springframework.oxm
hierarchy.protected Marshaller
createMarshaller()
Return a newly created JAXB marshaller.protected Unmarshaller
createUnmarshaller()
Return a newly created JAXB unmarshaller.Class<?>[]
getClassesToBeBound()
Return the list of Java classes to be recognized by a newly created JAXBContext.String
getContextPath()
Return the JAXB context path.JAXBContext
getJaxbContext()
Return the JAXBContext used by this marshaller, lazily building it if necessary.String[]
getPackagesToScan()
Return the packages to search for JAXB2 annotations.protected void
initJaxbMarshaller(Marshaller marshaller)
Template method that can be overridden by concrete JAXB marshallers for custom initialization behavior.protected void
initJaxbUnmarshaller(Unmarshaller unmarshaller)
Template method that can be overridden by concrete JAXB marshallers for custom initialization behavior.boolean
isProcessExternalEntities()
Return whether XML external entities are allowed.boolean
isSupportDtd()
Return whether DTD parsing is supported.void
marshal(Object graph, Result result)
Marshal the object graph with the given root into the providedResult
.void
marshal(Object graph, Result result, MimeContainer mimeContainer)
Marshals the object graph with the given root into the providedResult
, writing binary data to aMimeContainer
.void
setAdapters(<any>... adapters)
Specify theXmlAdapter
s to be registered with the JAXBMarshaller
andUnmarshaller
.void
setBeanClassLoader(ClassLoader classLoader)
Callback that supplies the beanclass loader
to a bean instance.void
setCheckForXmlRootElement(boolean checkForXmlRootElement)
Specify whether thesupports(Class)
should check for@XmlRootElement
annotations.void
setClassesToBeBound(Class<?>... classesToBeBound)
Set the list of Java classes to be recognized by a newly created JAXBContext.void
setContextPath(String contextPath)
Set a JAXB context path.void
setContextPaths(String... contextPaths)
Set multiple JAXB context paths.void
setJaxbContextProperties(Map<String,?> jaxbContextProperties)
Set theJAXBContext
properties.void
setLazyInit(boolean lazyInit)
Set whether to lazily initialize theJAXBContext
for this marshaller.void
setMappedClass(Class<?> mappedClass)
Specify a JAXB mapped class for partial unmarshalling.void
setMarshallerListener(Marshaller.Listener marshallerListener)
Specify theMarshaller.Listener
to be registered with the JAXBMarshaller
.void
setMarshallerProperties(Map<String,?> properties)
Set the JAXBMarshaller
properties.void
setMtomEnabled(boolean mtomEnabled)
Specify whether MTOM support should be enabled or not.void
setPackagesToScan(String... packagesToScan)
Set the packages to search for classes with JAXB2 annotations in the classpath.void
setProcessExternalEntities(boolean processExternalEntities)
Indicate whether external XML entities are processed when unmarshalling.void
setSchema(Resource schemaResource)
Set the schema resource to use for validation.void
setSchemaLanguage(String schemaLanguage)
Set the schema language.void
setSchemaResourceResolver(LSResourceResolver schemaResourceResolver)
Set the resource resolver, as used to load the schema resources.void
setSchemas(Resource... schemaResources)
Set the schema resources to use for validation.void
setSupportDtd(boolean supportDtd)
Indicate whether DTD parsing should be supported.void
setSupportJaxbElementClass(boolean supportJaxbElementClass)
void
setUnmarshallerListener(Unmarshaller.Listener unmarshallerListener)
Set theUnmarshaller.Listener
to be registered with the JAXBUnmarshaller
.void
setUnmarshallerProperties(Map<String,?> properties)
Set the JAXBUnmarshaller
properties.void
setValidationEventHandler(ValidationEventHandler validationEventHandler)
Set the JAXB validation event handler.boolean
supports(Class<?> clazz)
Indicate whether this marshaller can marshal instances of the supplied type.boolean
supports(Type genericType)
Indicates whether this marshaller can marshal instances of the supplied generic type.Object
unmarshal(Source source)
Unmarshal the givenSource
into an object graph.Object
unmarshal(Source source, MimeContainer mimeContainer)
Unmarshals the given providedSource
into an object graph, reading binary attachments from aMimeContainer
.protected Object
unmarshalStaxSource(Unmarshaller jaxbUnmarshaller, Source staxSource)
构造器详细资料
Jaxb2Marshaller
public Jaxb2Marshaller()
方法详细资料
setContextPaths
public void setContextPaths(String... contextPaths)
Set multiple JAXB context paths. The given array of context paths gets converted to a colon-delimited string, as supported by JAXB.
setContextPath
public void setContextPath(String contextPath)
Set a JAXB context path.Setting either this property,
"classesToBeBound"
or"packagesToScan"
is required.
getContextPath
public String getContextPath()
Return the JAXB context path.
setClassesToBeBound
public void setClassesToBeBound(Class<?>... classesToBeBound)
Set the list of Java classes to be recognized by a newly created JAXBContext.Setting either this property,
"contextPath"
or"packagesToScan"
is required.
getClassesToBeBound
public Class<?>[] getClassesToBeBound()
Return the list of Java classes to be recognized by a newly created JAXBContext.
setPackagesToScan
public void setPackagesToScan(String... packagesToScan)
Set the packages to search for classes with JAXB2 annotations in the classpath. This is using a Spring-bases search and therefore analogous to Spring's component-scan feature (ClassPathBeanDefinitionScanner
).Setting either this property,
"contextPath"
or"classesToBeBound"
is required.
getPackagesToScan
public String[] getPackagesToScan()
Return the packages to search for JAXB2 annotations.
setJaxbContextProperties
public void setJaxbContextProperties(Map<String,?> jaxbContextProperties)
Set theJAXBContext
properties. These implementation-specific properties will be set on the underlyingJAXBContext
.
setMarshallerProperties
public void setMarshallerProperties(Map<String,?> properties)
Set the JAXBMarshaller
properties.These properties will be set on the underlying JAXB
Marshaller
, and allow for features such as indentation.- 参数:
properties
- the properties- 另请参阅:
javax.xml.bind.Marshaller#setProperty(String, Object)
,javax.xml.bind.Marshaller#JAXB_ENCODING
,javax.xml.bind.Marshaller#JAXB_FORMATTED_OUTPUT
,javax.xml.bind.Marshaller#JAXB_NO_NAMESPACE_SCHEMA_LOCATION
,javax.xml.bind.Marshaller#JAXB_SCHEMA_LOCATION
setUnmarshallerProperties
public void setUnmarshallerProperties(Map<String,?> properties)
Set the JAXBUnmarshaller
properties.These properties will be set on the underlying JAXB
Unmarshaller
.- 参数:
properties
- the properties- 另请参阅:
javax.xml.bind.Unmarshaller#setProperty(String, Object)
setMarshallerListener
public void setMarshallerListener(Marshaller.Listener marshallerListener)
Specify theMarshaller.Listener
to be registered with the JAXBMarshaller
.
setUnmarshallerListener
public void setUnmarshallerListener(Unmarshaller.Listener unmarshallerListener)
Set theUnmarshaller.Listener
to be registered with the JAXBUnmarshaller
.
setValidationEventHandler
public void setValidationEventHandler(ValidationEventHandler validationEventHandler)
Set the JAXB validation event handler. This event handler will be called by JAXB if any validation errors are encountered during calls to any of the marshal APIs.
setAdapters
public void setAdapters(<any>... adapters)
Specify theXmlAdapter
s to be registered with the JAXBMarshaller
andUnmarshaller
.
setSchema
public void setSchema(Resource schemaResource)
Set the schema resource to use for validation.
setSchemas
public void setSchemas(Resource... schemaResources)
Set the schema resources to use for validation.
setSchemaLanguage
public void setSchemaLanguage(String schemaLanguage)
Set the schema language. Default is the W3C XML Schema:http://www.w3.org/2001/XMLSchema"
.
setSchemaResourceResolver
public void setSchemaResourceResolver(LSResourceResolver schemaResourceResolver)
Set the resource resolver, as used to load the schema resources.
setLazyInit
public void setLazyInit(boolean lazyInit)
Set whether to lazily initialize theJAXBContext
for this marshaller. Default isfalse
to initialize on startup; can be switched totrue
.Early initialization just applies if
afterPropertiesSet()
is called.
setMtomEnabled
public void setMtomEnabled(boolean mtomEnabled)
Specify whether MTOM support should be enabled or not. Default isfalse
: marshalling using XOP/MTOM not being enabled.
setSupportJaxbElementClass
public void setSupportJaxbElementClass(boolean supportJaxbElementClass)
Specify whether thesupports(Class)
returnstrue
for theJAXBElement
class.Default is
false
, meaning thatsupports(Class)
always returnsfalse
forJAXBElement
classes (thoughsupports(Type)
can returntrue
, since it can obtain the type parameters ofJAXBElement
).This property is typically enabled in combination with usage of classes like
MarshallingView
, since theModelAndView
does not offer type parameter information at runtime.
setCheckForXmlRootElement
public void setCheckForXmlRootElement(boolean checkForXmlRootElement)
Specify whether thesupports(Class)
should check for@XmlRootElement
annotations.Default is
true
, meaning thatsupports(Class)
will check for this annotation. However, some JAXB implementations (i.e. EclipseLink MOXy) allow for defining the bindings in an external definition file, thus keeping the classes annotations free. Setting this property tofalse
supports these JAXB implementations.
setMappedClass
public void setMappedClass(Class<?> mappedClass)
Specify a JAXB mapped class for partial unmarshalling.- 另请参阅:
javax.xml.bind.Unmarshaller#unmarshal(javax.xml.transform.Source, Class)
setSupportDtd
public void setSupportDtd(boolean supportDtd)
Indicate whether DTD parsing should be supported.Default is
false
meaning that DTD is disabled.
isSupportDtd
public boolean isSupportDtd()
Return whether DTD parsing is supported.
setProcessExternalEntities
public void setProcessExternalEntities(boolean processExternalEntities)
Indicate whether external XML entities are processed when unmarshalling.Default is
false
, meaning that external entities are not resolved. Note that processing of external entities will only be enabled/disabled when theSource
passed tounmarshal(Source)
is aSAXSource
orStreamSource
. It has no effect forDOMSource
orStAXSource
instances.Note: setting this option to
true
also automatically setssetSupportDtd(boolean)
totrue
.
isProcessExternalEntities
public boolean isProcessExternalEntities()
Return whether XML external entities are allowed.
setBeanClassLoader
public void setBeanClassLoader(ClassLoader classLoader)
从接口复制的说明:BeanClassLoaderAware
Callback that supplies the beanclass loader
to a bean instance.Invoked after the population of normal bean properties but before an initialization callback such as
InitializingBean's
InitializingBean.afterPropertiesSet()
method or a custom init-method.- 指定者:
setBeanClassLoader
在接口中BeanClassLoaderAware
- 参数:
classLoader
- the owning class loader; may benull
in which case a defaultClassLoader
must be used, for example theClassLoader
obtained viaClassUtils.getDefaultClassLoader()
afterPropertiesSet
public void afterPropertiesSet() throws Exception
从接口复制的说明:InitializingBean
Invoked by the containingBeanFactory
after it has set all bean properties and satisfiedBeanFactoryAware
,ApplicationContextAware
etc.This method allows the bean instance to perform validation of its overall configuration and final initialization when all bean properties have been set.
- 指定者:
afterPropertiesSet
在接口中InitializingBean
- 抛出:
Exception
- in the event of misconfiguration (such as failure to set an essential property) or if initialization fails for any other reason
getJaxbContext
public JAXBContext getJaxbContext()
Return the JAXBContext used by this marshaller, lazily building it if necessary.
supports
public boolean supports(Class<?> clazz)
从接口复制的说明:Marshaller
Indicate whether this marshaller can marshal instances of the supplied type.- 指定者:
supports
在接口中Marshaller
- 指定者:
supports
在接口中Unmarshaller
- 参数:
clazz
- the class that this marshaller is being asked if it can marshal- 返回:
true
if this marshaller can indeed marshal instances of the supplied class;false
otherwise
supports
public boolean supports(Type genericType)
从接口复制的说明:GenericMarshaller
Indicates whether this marshaller can marshal instances of the supplied generic type.- 指定者:
supports
在接口中GenericMarshaller
- 指定者:
supports
在接口中GenericUnmarshaller
- 参数:
genericType
- the type that this marshaller is being asked if it can marshal- 返回:
true
if this marshaller can indeed marshal instances of the supplied type;false
otherwise
marshal
public void marshal(Object graph, Result result) throws XmlMappingException
从接口复制的说明:Marshaller
Marshal the object graph with the given root into the providedResult
.- 指定者:
marshal
在接口中Marshaller
- 参数:
graph
- the root of the object graph to marshalresult
- the result to marshal to- 抛出:
XmlMappingException
- if the given object cannot be marshalled to the result
marshal
public void marshal(Object graph, Result result, MimeContainer mimeContainer) throws XmlMappingException
从接口复制的说明:MimeMarshaller
Marshals the object graph with the given root into the providedResult
, writing binary data to aMimeContainer
.- 指定者:
marshal
在接口中MimeMarshaller
- 参数:
graph
- the root of the object graph to marshalresult
- the result to marshal tomimeContainer
- the MIME container to write extracted binary content to- 抛出:
XmlMappingException
- if the given object cannot be marshalled to the result
createMarshaller
protected Marshaller createMarshaller()
Return a newly created JAXB marshaller.Note: JAXB marshallers are not necessarily thread-safe.
initJaxbMarshaller
protected void initJaxbMarshaller(Marshaller marshaller) throws JAXBException
Template method that can be overridden by concrete JAXB marshallers for custom initialization behavior. Gets called after creation of JAXBMarshaller
, and after the respective properties have been set.The default implementation sets the
defined properties
, thevalidation event handler
, theschemas
,listener
, andadapters
.- 抛出:
JAXBException
unmarshal
public Object unmarshal(Source source) throws XmlMappingException
从接口复制的说明:Unmarshaller
Unmarshal the givenSource
into an object graph.- 指定者:
unmarshal
在接口中Unmarshaller
- 参数:
source
- the source to marshal from- 返回:
- the object graph
- 抛出:
XmlMappingException
- if the given source cannot be mapped to an object
unmarshal
public Object unmarshal(Source source, MimeContainer mimeContainer) throws XmlMappingException
从接口复制的说明:MimeUnmarshaller
Unmarshals the given providedSource
into an object graph, reading binary attachments from aMimeContainer
.- 指定者:
unmarshal
在接口中MimeUnmarshaller
- 参数:
source
- the source to marshal frommimeContainer
- the MIME container to read extracted binary content from- 返回:
- the object graph
- 抛出:
XmlMappingException
- if the given source cannot be mapped to an object
createUnmarshaller
protected Unmarshaller createUnmarshaller()
Return a newly created JAXB unmarshaller.Note: JAXB unmarshallers are not necessarily thread-safe.
unmarshalStaxSource
protected Object unmarshalStaxSource(Unmarshaller jaxbUnmarshaller, Source staxSource) throws JAXBException
- 抛出:
JAXBException
initJaxbUnmarshaller
protected void initJaxbUnmarshaller(Unmarshaller unmarshaller) throws JAXBException
Template method that can be overridden by concrete JAXB marshallers for custom initialization behavior. Gets called after creation of JAXBMarshaller
, and after the respective properties have been set.The default implementation sets the
defined properties
, thevalidation event handler
, theschemas
,listener
, andadapters
.- 抛出:
JAXBException
convertJaxbException
protected XmlMappingException convertJaxbException(JAXBException ex)
Convert the givenJAXBException
to an appropriate exception from theorg.springframework.oxm
hierarchy.- 参数:
ex
-JAXBException
that occurred- 返回:
- the corresponding
XmlMappingException