Class Jaxb2Marshaller
- All Implemented Interfaces:
- Aware,- BeanClassLoaderAware,- InitializingBean,- GenericMarshaller,- GenericUnmarshaller,- Marshaller,- MimeMarshaller,- MimeUnmarshaller,- Unmarshaller
GenericMarshaller interface for JAXB 2.2.
 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.
- Since:
- 3.0
- Author:
- Arjen Poutsma, Juergen Hoeller, Rossen Stoyanchev, Sam Brannen
- See Also:
- 
- 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(jakarta.xml.bind.Marshaller.Listener)
- setUnmarshallerListener(jakarta.xml.bind.Unmarshaller.Listener)
- setAdapters(jakarta.xml.bind.annotation.adapters.XmlAdapter<?, ?>...)
 
- 
Field SummaryFieldsModifier and TypeFieldDescriptionprotected final org.apache.commons.logging.LogLogger available to subclasses.
- 
Constructor SummaryConstructors
- 
Method SummaryModifier and TypeMethodDescriptionvoidInvoked by the containingBeanFactoryafter it has set all bean properties and satisfiedBeanFactoryAware,ApplicationContextAwareetc.protected XmlMappingExceptionconvertJaxbException(jakarta.xml.bind.JAXBException ex) Convert the givenJAXBExceptionto an appropriate exception from theorg.springframework.oxmhierarchy.jakarta.xml.bind.MarshallerReturn a newly created JAXB marshaller.jakarta.xml.bind.UnmarshallerReturn a newly created JAXB unmarshaller.Return the list of Java classes to be recognized by a newly created JAXBContext.Return the JAXB context path.jakarta.xml.bind.JAXBContextReturn the JAXBContext used by this marshaller, lazily building it if necessary.Return the packages to search for JAXB2 annotations.protected voidinitJaxbMarshaller(jakarta.xml.bind.Marshaller marshaller) Template method that can be overridden by concrete JAXB marshallers for custom initialization behavior.protected voidinitJaxbUnmarshaller(jakarta.xml.bind.Unmarshaller unmarshaller) Template method that can be overridden by concrete JAXB marshallers for custom initialization behavior.booleanReturn whether XML external entities are allowed.booleanReturn whether DTD parsing is supported.voidMarshal the object graph with the given root into the providedResult.voidmarshal(Object graph, Result result, @Nullable MimeContainer mimeContainer) Marshals the object graph with the given root into the providedResult, writing binary data to aMimeContainer.voidsetAdapters(jakarta.xml.bind.annotation.adapters.XmlAdapter<?, ?>... adapters) Specify theXmlAdapters to be registered with the JAXBMarshallerandUnmarshaller.voidsetBeanClassLoader(ClassLoader classLoader) Callback that supplies the beanclass loaderto a bean instance.voidsetCheckForXmlRootElement(boolean checkForXmlRootElement) Specify whether thesupports(Class)should check for@XmlRootElementannotations.voidsetClassesToBeBound(Class<?> @Nullable ... classesToBeBound) Set the list of Java classes to be recognized by a newly created JAXBContext.voidsetContextPath(@Nullable String contextPath) Set a JAXB context path.voidsetContextPaths(String... contextPaths) Set multiple JAXB context paths.voidsetJaxbContextProperties(Map<String, ?> jaxbContextProperties) Set theJAXBContextproperties.voidsetLazyInit(boolean lazyInit) Set whether to lazily initialize theJAXBContextfor this marshaller.voidsetMappedClass(Class<?> mappedClass) Specify a JAXB mapped class for partial unmarshalling.voidsetMarshallerListener(jakarta.xml.bind.Marshaller.Listener marshallerListener) Specify theMarshaller.Listenerto be registered with the JAXBMarshaller.voidsetMarshallerProperties(Map<String, ?> properties) Set the JAXBMarshallerproperties.voidsetMtomEnabled(boolean mtomEnabled) Specify whether MTOM support should be enabled or not.voidsetPackagesToScan(String @Nullable ... packagesToScan) Set the packages to search for classes with JAXB2 annotations in the classpath.voidsetProcessExternalEntities(boolean processExternalEntities) Indicate whether external XML entities are processed when unmarshalling.voidSet the schema resource to use for validation.voidsetSchemaLanguage(String schemaLanguage) Set the schema language.voidsetSchemaResourceResolver(LSResourceResolver schemaResourceResolver) Set the resource resolver, as used to load the schema resources.voidsetSchemas(Resource... schemaResources) Set the schema resources to use for validation.voidsetSupportDtd(boolean supportDtd) Indicate whether DTD parsing should be supported.voidsetSupportJaxbElementClass(boolean supportJaxbElementClass) voidsetUnmarshallerListener(jakarta.xml.bind.Unmarshaller.Listener unmarshallerListener) Set theUnmarshaller.Listenerto be registered with the JAXBUnmarshaller.voidsetUnmarshallerProperties(Map<String, ?> properties) Set the JAXBUnmarshallerproperties.voidsetValidationEventHandler(jakarta.xml.bind.ValidationEventHandler validationEventHandler) Set the JAXB validation event handler.booleanIndicate whether this marshaller can marshal instances of the supplied type.booleanIndicates whether this marshaller can marshal instances of the supplied generic type.Unmarshal the givenSourceinto an object graph.unmarshal(Source source, @Nullable MimeContainer mimeContainer) Unmarshals the given providedSourceinto an object graph, reading binary attachments from aMimeContainer.protected ObjectunmarshalStaxSource(jakarta.xml.bind.Unmarshaller jaxbUnmarshaller, Source staxSource) 
- 
Field Details- 
loggerprotected final org.apache.commons.logging.Log loggerLogger available to subclasses.
 
- 
- 
Constructor Details- 
Jaxb2Marshallerpublic Jaxb2Marshaller()
 
- 
- 
Method Details- 
setContextPathsSet multiple JAXB context paths. The given array of context paths gets converted to a colon-delimited string, as supported by JAXB.
- 
setContextPathSet a JAXB context path.Setting either this property, "classesToBeBound"or"packagesToScan"is required.
- 
getContextPathReturn the JAXB context path.
- 
setClassesToBeBoundSet the list of Java classes to be recognized by a newly created JAXBContext.Setting either this property, "contextPath"or"packagesToScan"is required.
- 
getClassesToBeBoundReturn the list of Java classes to be recognized by a newly created JAXBContext.
- 
setPackagesToScanSet 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.
- 
getPackagesToScanReturn the packages to search for JAXB2 annotations.
- 
setJaxbContextPropertiesSet theJAXBContextproperties. These implementation-specific properties will be set on the underlyingJAXBContext.
- 
setMarshallerPropertiesSet the JAXBMarshallerproperties.These properties will be set on the underlying JAXB Marshaller, and allow for features such as indentation.- Parameters:
- properties- the properties
- See Also:
- 
- Marshaller.setProperty(String, Object)
- Marshaller.JAXB_ENCODING
- Marshaller.JAXB_FORMATTED_OUTPUT
- Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION
- Marshaller.JAXB_SCHEMA_LOCATION
 
 
- 
setUnmarshallerPropertiesSet the JAXBUnmarshallerproperties.These properties will be set on the underlying JAXB Unmarshaller.- Parameters:
- properties- the properties
- See Also:
- 
- Unmarshaller.setProperty(String, Object)
 
 
- 
setMarshallerListenerpublic void setMarshallerListener(jakarta.xml.bind.Marshaller.Listener marshallerListener) Specify theMarshaller.Listenerto be registered with the JAXBMarshaller.
- 
setUnmarshallerListenerpublic void setUnmarshallerListener(jakarta.xml.bind.Unmarshaller.Listener unmarshallerListener) Set theUnmarshaller.Listenerto be registered with the JAXBUnmarshaller.
- 
setValidationEventHandlerpublic void setValidationEventHandler(jakarta.xml.bind.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.
- 
setAdapterspublic void setAdapters(jakarta.xml.bind.annotation.adapters.XmlAdapter<?, ?>... adapters) Specify theXmlAdapters to be registered with the JAXBMarshallerandUnmarshaller.
- 
setSchemaSet the schema resource to use for validation.
- 
setSchemasSet the schema resources to use for validation.
- 
setSchemaLanguageSet the schema language. Default is the W3C XML Schema:http://www.w3.org/2001/XMLSchema".
- 
setSchemaResourceResolverSet the resource resolver, as used to load the schema resources.
- 
setLazyInitpublic void setLazyInit(boolean lazyInit) Set whether to lazily initialize theJAXBContextfor this marshaller. Default isfalseto initialize on startup; can be switched totrue.Early initialization just applies if afterPropertiesSet()is called.
- 
setMtomEnabledpublic void setMtomEnabled(boolean mtomEnabled) Specify whether MTOM support should be enabled or not. Default isfalse: marshalling using XOP/MTOM not being enabled.
- 
setSupportJaxbElementClasspublic void setSupportJaxbElementClass(boolean supportJaxbElementClass) Specify whether thesupports(Class)returnstruefor theJAXBElementclass.Default is false, meaning thatsupports(Class)always returnsfalseforJAXBElementclasses (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 theModelAndViewdoes not offer type parameter information at runtime.- See Also:
 
- 
setCheckForXmlRootElementpublic void setCheckForXmlRootElement(boolean checkForXmlRootElement) Specify whether thesupports(Class)should check for@XmlRootElementannotations.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 tofalsesupports these JAXB implementations.- See Also:
 
- 
setMappedClassSpecify a JAXB mapped class for partial unmarshalling.- See Also:
- 
- Unmarshaller.unmarshal(javax.xml.transform.Source, Class)
 
 
- 
setSupportDtdpublic void setSupportDtd(boolean supportDtd) Indicate whether DTD parsing should be supported.Default is falsemeaning that DTD is disabled.
- 
isSupportDtdpublic boolean isSupportDtd()Return whether DTD parsing is supported.
- 
setProcessExternalEntitiespublic 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 theSourcepassed tounmarshal(Source)is aSAXSourceorStreamSource. It has no effect forDOMSourceorStAXSourceinstances.Note: setting this option to truealso automatically setssetSupportDtd(boolean)totrue.
- 
isProcessExternalEntitiespublic boolean isProcessExternalEntities()Return whether XML external entities are allowed.
- 
setBeanClassLoaderDescription copied from interface:BeanClassLoaderAwareCallback that supplies the beanclass loaderto a bean instance.Invoked after the population of normal bean properties but before an initialization callback such as InitializingBean'sInitializingBean.afterPropertiesSet()method or a custom init-method.- Specified by:
- setBeanClassLoaderin interface- BeanClassLoaderAware
- Parameters:
- classLoader- the owning class loader
 
- 
afterPropertiesSetDescription copied from interface:InitializingBeanInvoked by the containingBeanFactoryafter it has set all bean properties and satisfiedBeanFactoryAware,ApplicationContextAwareetc.This method allows the bean instance to perform validation of its overall configuration and final initialization when all bean properties have been set. - Specified by:
- afterPropertiesSetin interface- InitializingBean
- Throws:
- Exception- in the event of misconfiguration (such as failure to set an essential property) or if initialization fails for any other reason
 
- 
getJaxbContextpublic jakarta.xml.bind.JAXBContext getJaxbContext()Return the JAXBContext used by this marshaller, lazily building it if necessary.
- 
supportsDescription copied from interface:MarshallerIndicate whether this marshaller can marshal instances of the supplied type.- Specified by:
- supportsin interface- Marshaller
- Specified by:
- supportsin interface- Unmarshaller
- Parameters:
- clazz- the class that this marshaller is being asked if it can marshal
- Returns:
- trueif this marshaller can indeed marshal instances of the supplied class;- falseotherwise
 
- 
supportsDescription copied from interface:GenericMarshallerIndicates whether this marshaller can marshal instances of the supplied generic type.- Specified by:
- supportsin interface- GenericMarshaller
- Specified by:
- supportsin interface- GenericUnmarshaller
- Parameters:
- genericType- the type that this marshaller is being asked if it can marshal
- Returns:
- trueif this marshaller can indeed marshal instances of the supplied type;- falseotherwise
 
- 
marshalDescription copied from interface:MarshallerMarshal the object graph with the given root into the providedResult.- Specified by:
- marshalin interface- Marshaller
- Parameters:
- graph- the root of the object graph to marshal
- result- the result to marshal to
- Throws:
- XmlMappingException- if the given object cannot be marshalled to the result
 
- 
marshalpublic void marshal(Object graph, Result result, @Nullable MimeContainer mimeContainer) throws XmlMappingException Description copied from interface:MimeMarshallerMarshals the object graph with the given root into the providedResult, writing binary data to aMimeContainer.- Specified by:
- marshalin interface- MimeMarshaller
- Parameters:
- graph- the root of the object graph to marshal
- result- the result to marshal to
- mimeContainer- the MIME container to write extracted binary content to
- Throws:
- XmlMappingException- if the given object cannot be marshalled to the result
 
- 
createMarshallerpublic jakarta.xml.bind.Marshaller createMarshaller()Return a newly created JAXB marshaller.Note: JAXB marshallers are not necessarily thread-safe. This method is public as of 5.2. - Since:
- 5.2
- See Also:
 
- 
initJaxbMarshallerprotected void initJaxbMarshaller(jakarta.xml.bind.Marshaller marshaller) throws jakarta.xml.bind.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.- Throws:
- jakarta.xml.bind.JAXBException
 
- 
unmarshalDescription copied from interface:UnmarshallerUnmarshal the givenSourceinto an object graph.- Specified by:
- unmarshalin interface- Unmarshaller
- Parameters:
- source- the source to marshal from
- Returns:
- the object graph
- Throws:
- XmlMappingException- if the given source cannot be mapped to an object
 
- 
unmarshalpublic Object unmarshal(Source source, @Nullable MimeContainer mimeContainer) throws XmlMappingException Description copied from interface:MimeUnmarshallerUnmarshals the given providedSourceinto an object graph, reading binary attachments from aMimeContainer.- Specified by:
- unmarshalin interface- MimeUnmarshaller
- Parameters:
- source- the source to marshal from
- mimeContainer- the MIME container to read extracted binary content from
- Returns:
- the object graph
- Throws:
- XmlMappingException- if the given source cannot be mapped to an object
 
- 
createUnmarshallerpublic jakarta.xml.bind.Unmarshaller createUnmarshaller()Return a newly created JAXB unmarshaller.Note: JAXB unmarshallers are not necessarily thread-safe. This method is public as of 5.2. - Since:
- 5.2
- See Also:
 
- 
unmarshalStaxSourceprotected Object unmarshalStaxSource(jakarta.xml.bind.Unmarshaller jaxbUnmarshaller, Source staxSource) throws jakarta.xml.bind.JAXBException - Throws:
- jakarta.xml.bind.JAXBException
 
- 
initJaxbUnmarshallerprotected void initJaxbUnmarshaller(jakarta.xml.bind.Unmarshaller unmarshaller) throws jakarta.xml.bind.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.- Throws:
- jakarta.xml.bind.JAXBException
 
- 
convertJaxbExceptionConvert the givenJAXBExceptionto an appropriate exception from theorg.springframework.oxmhierarchy.- Parameters:
- ex-- JAXBExceptionthat occurred
- Returns:
- the corresponding XmlMappingException
 
 
-