public class Jaxb2Marshaller extends java.lang.Object implements MimeMarshaller, MimeUnmarshaller, GenericMarshaller, GenericUnmarshaller, BeanClassLoaderAware, ResourceLoaderAware, InitializingBean
Marshaller
interface for JAXB 2.0.
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.
setContextPath(String)
,
setClassesToBeBound(Class[])
,
setJaxbContextProperties(Map)
,
setMarshallerProperties(Map)
,
setUnmarshallerProperties(Map)
,
setSchema(Resource)
,
setSchemas(Resource[])
,
setMarshallerListener(javax.xml.bind.Marshaller.Listener)
,
setUnmarshallerListener(javax.xml.bind.Unmarshaller.Listener)
,
setAdapters(XmlAdapter[])
Modifier and Type | Class and Description |
---|---|
private static class |
Jaxb2Marshaller.ByteArrayDataSource
DataSource that wraps around a byte array.
|
private static class |
Jaxb2Marshaller.Jaxb2AttachmentMarshaller |
private static class |
Jaxb2Marshaller.Jaxb2AttachmentUnmarshaller |
Modifier and Type | Field and Description |
---|---|
private javax.xml.bind.annotation.adapters.XmlAdapter<?,?>[] |
adapters |
private java.lang.ClassLoader |
beanClassLoader |
private boolean |
checkForXmlRootElement |
private static java.lang.String |
CID |
private java.lang.Class<?>[] |
classesToBeBound |
private java.lang.String |
contextPath |
private javax.xml.bind.JAXBContext |
jaxbContext |
private java.util.Map<java.lang.String,?> |
jaxbContextProperties |
private boolean |
lazyInit |
protected Log |
logger
Logger available to subclasses.
|
private javax.xml.bind.Marshaller.Listener |
marshallerListener |
private java.util.Map<java.lang.String,?> |
marshallerProperties |
private boolean |
mtomEnabled |
private java.lang.String[] |
packagesToScan |
private ResourceLoader |
resourceLoader |
private javax.xml.validation.Schema |
schema |
private java.lang.String |
schemaLanguage |
private org.w3c.dom.ls.LSResourceResolver |
schemaResourceResolver |
private Resource[] |
schemaResources |
private boolean |
supportJaxbElementClass |
private javax.xml.bind.Unmarshaller.Listener |
unmarshallerListener |
private java.util.Map<java.lang.String,?> |
unmarshallerProperties |
private javax.xml.bind.ValidationEventHandler |
validationEventHandler |
Constructor and Description |
---|
Jaxb2Marshaller() |
Modifier and Type | Method and Description |
---|---|
void |
afterPropertiesSet()
Invoked by a BeanFactory after it has set all bean properties supplied
(and satisfied BeanFactoryAware and ApplicationContextAware).
|
protected XmlMappingException |
convertJaxbException(javax.xml.bind.JAXBException ex)
Convert the given
JAXBException to an appropriate exception from the
org.springframework.oxm hierarchy. |
private javax.xml.bind.JAXBContext |
createJaxbContextFromClasses() |
private javax.xml.bind.JAXBContext |
createJaxbContextFromContextPath() |
private javax.xml.bind.JAXBContext |
createJaxbContextFromPackages() |
protected javax.xml.bind.Marshaller |
createMarshaller()
Return a newly created JAXB marshaller.
|
protected javax.xml.bind.Unmarshaller |
createUnmarshaller()
Return a newly created JAXB unmarshaller.
|
java.lang.Class<?>[] |
getClassesToBeBound()
Return the list of Java classes to be recognized by a newly created JAXBContext.
|
java.lang.String |
getContextPath()
Return the JAXB context path.
|
protected javax.xml.bind.JAXBContext |
getJaxbContext() |
java.lang.String[] |
getPackagesToScan()
Return the packages to search for JAXB2 annotations.
|
protected void |
initJaxbMarshaller(javax.xml.bind.Marshaller marshaller)
Template method that can be overridden by concrete JAXB marshallers for custom initialization behavior.
|
protected void |
initJaxbUnmarshaller(javax.xml.bind.Unmarshaller unmarshaller)
Template method that can be overridden by concrete JAXB marshallers for custom initialization behavior.
|
private boolean |
isPrimitiveWrapper(java.lang.Class<?> clazz)
Checks whether the given type is a primitive wrapper type.
|
private boolean |
isStandardClass(java.lang.Class<?> clazz)
Checks whether the given type is a standard class.
|
private javax.xml.validation.Schema |
loadSchema(Resource[] resources,
java.lang.String schemaLanguage) |
void |
marshal(java.lang.Object graph,
javax.xml.transform.Result result)
Marshals the object graph with the given root into the provided
Result . |
void |
marshal(java.lang.Object graph,
javax.xml.transform.Result result,
MimeContainer mimeContainer)
Marshals the object graph with the given root into the provided
Result ,
writing binary data to a MimeContainer . |
private void |
marshalStaxResult(javax.xml.bind.Marshaller jaxbMarshaller,
java.lang.Object graph,
javax.xml.transform.Result staxResult) |
void |
setAdapters(javax.xml.bind.annotation.adapters.XmlAdapter<?,?>[] adapters)
Specify the
XmlAdapter s to be registered with the JAXB Marshaller
and Unmarshaller |
void |
setBeanClassLoader(java.lang.ClassLoader classLoader)
Callback that supplies the bean
class loader to
a bean instance. |
void |
setCheckForXmlRootElement(boolean checkForXmlRootElement)
Specify whether the
supports(Class) should check for
@XmlRootElement annotations. |
void |
setClassesToBeBound(java.lang.Class<?>... classesToBeBound)
Set the list of Java classes to be recognized by a newly created JAXBContext.
|
void |
setContextPath(java.lang.String contextPath)
Set a JAXB context path.
|
void |
setContextPaths(java.lang.String... contextPaths)
Set multiple JAXB context paths.
|
void |
setJaxbContextProperties(java.util.Map<java.lang.String,?> jaxbContextProperties)
Set the
JAXBContext properties. |
void |
setLazyInit(boolean lazyInit)
Set whether to lazily initialize the
JAXBContext for this marshaller. |
void |
setMarshallerListener(javax.xml.bind.Marshaller.Listener marshallerListener)
Specify the
Marshaller.Listener to be registered with the JAXB Marshaller . |
void |
setMarshallerProperties(java.util.Map<java.lang.String,?> properties)
Set the JAXB
Marshaller properties. |
void |
setMtomEnabled(boolean mtomEnabled)
Specify whether MTOM support should be enabled or not.
|
void |
setPackagesToScan(java.lang.String[] packagesToScan)
Set the packages to search using Spring-based scanning for classes with JAXB2 annotations in the classpath.
|
void |
setResourceLoader(ResourceLoader resourceLoader)
Set the ResourceLoader that this object runs in.
|
void |
setSchema(Resource schemaResource)
Set the schema resource to use for validation.
|
void |
setSchemaLanguage(java.lang.String schemaLanguage)
Set the schema language.
|
void |
setSchemaResourceResolver(org.w3c.dom.ls.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 |
setSupportJaxbElementClass(boolean supportJaxbElementClass)
|
void |
setUnmarshallerListener(javax.xml.bind.Unmarshaller.Listener unmarshallerListener)
Set the
Unmarshaller.Listener to be registered with the JAXB Unmarshaller . |
void |
setUnmarshallerProperties(java.util.Map<java.lang.String,?> properties)
Set the JAXB
Unmarshaller properties. |
void |
setValidationEventHandler(javax.xml.bind.ValidationEventHandler validationEventHandler)
Set the JAXB validation event handler.
|
boolean |
supports(java.lang.Class<?> clazz)
Indicates whether this marshaller can marshal instances of the supplied type.
|
boolean |
supports(java.lang.reflect.Type genericType)
Indicates whether this marshaller can marshal instances of the supplied generic type.
|
private boolean |
supportsInternal(java.lang.Class<?> clazz,
boolean checkForXmlRootElement) |
java.lang.Object |
unmarshal(javax.xml.transform.Source source)
Unmarshals the given
Source into an object graph. |
java.lang.Object |
unmarshal(javax.xml.transform.Source source,
MimeContainer mimeContainer)
Unmarshals the given provided
Source into an object graph,
reading binary attachments from a MimeContainer . |
private java.lang.Object |
unmarshalStaxSource(javax.xml.bind.Unmarshaller jaxbUnmarshaller,
javax.xml.transform.Source staxSource) |
private static final java.lang.String CID
protected final Log logger
private java.lang.String contextPath
private java.lang.Class<?>[] classesToBeBound
private java.lang.String[] packagesToScan
private java.util.Map<java.lang.String,?> jaxbContextProperties
private java.util.Map<java.lang.String,?> marshallerProperties
private java.util.Map<java.lang.String,?> unmarshallerProperties
private javax.xml.bind.Marshaller.Listener marshallerListener
private javax.xml.bind.Unmarshaller.Listener unmarshallerListener
private javax.xml.bind.ValidationEventHandler validationEventHandler
private javax.xml.bind.annotation.adapters.XmlAdapter<?,?>[] adapters
private Resource[] schemaResources
private java.lang.String schemaLanguage
private boolean mtomEnabled
private java.lang.ClassLoader beanClassLoader
private ResourceLoader resourceLoader
private javax.xml.bind.JAXBContext jaxbContext
private javax.xml.validation.Schema schema
private boolean lazyInit
private boolean supportJaxbElementClass
private boolean checkForXmlRootElement
private org.w3c.dom.ls.LSResourceResolver schemaResourceResolver
public void setContextPaths(java.lang.String... contextPaths)
public void setContextPath(java.lang.String contextPath)
Setting this property, "classesToBeBound"
, or
"packagesToScan"
is required.
public java.lang.String getContextPath()
public void setClassesToBeBound(java.lang.Class<?>... classesToBeBound)
Setting this property, "contextPath"
, or
"packagesToScan"
is required.
public java.lang.Class<?>[] getClassesToBeBound()
public void setPackagesToScan(java.lang.String[] packagesToScan)
Setting this property, "contextPath"
, or
"classesToBeBound"
is required. This is analogous to Spring's component-scan feature
(ClassPathBeanDefinitionScanner
).
public java.lang.String[] getPackagesToScan()
public void setJaxbContextProperties(java.util.Map<java.lang.String,?> jaxbContextProperties)
JAXBContext
properties. These implementation-specific
properties will be set on the underlying JAXBContext
.public void setMarshallerProperties(java.util.Map<java.lang.String,?> properties)
Marshaller
properties. These properties will be set on the
underlying JAXB Marshaller
, and allow for features such as indentation.properties
- the propertiesMarshaller.setProperty(String,Object)
,
Marshaller.JAXB_ENCODING
,
Marshaller.JAXB_FORMATTED_OUTPUT
,
Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION
,
Marshaller.JAXB_SCHEMA_LOCATION
public void setUnmarshallerProperties(java.util.Map<java.lang.String,?> properties)
Unmarshaller
properties. These properties will be set on the
underlying JAXB Unmarshaller
.properties
- the propertiesUnmarshaller.setProperty(String,Object)
public void setMarshallerListener(javax.xml.bind.Marshaller.Listener marshallerListener)
Marshaller.Listener
to be registered with the JAXB Marshaller
.public void setUnmarshallerListener(javax.xml.bind.Unmarshaller.Listener unmarshallerListener)
Unmarshaller.Listener
to be registered with the JAXB Unmarshaller
.public void setValidationEventHandler(javax.xml.bind.ValidationEventHandler validationEventHandler)
public void setAdapters(javax.xml.bind.annotation.adapters.XmlAdapter<?,?>[] adapters)
XmlAdapter
s to be registered with the JAXB Marshaller
and Unmarshaller
public void setSchema(Resource schemaResource)
public void setSchemas(Resource[] schemaResources)
public void setSchemaLanguage(java.lang.String schemaLanguage)
http://www.w3.org/2001/XMLSchema"
.XMLConstants.W3C_XML_SCHEMA_NS_URI
,
XMLConstants.RELAXNG_NS_URI
public void setSchemaResourceResolver(org.w3c.dom.ls.LSResourceResolver schemaResourceResolver)
SchemaFactory.setResourceResolver(org.w3c.dom.ls.LSResourceResolver)
,
setSchema(Resource)
,
setSchemas(Resource[])
public void setMtomEnabled(boolean mtomEnabled)
false
: marshalling using XOP/MTOM not being enabled.public void setLazyInit(boolean lazyInit)
JAXBContext
for this marshaller.
Default is false
to initialize on startup; can be switched to true
.
Early initialization just applies if afterPropertiesSet()
is called.
public void setSupportJaxbElementClass(boolean supportJaxbElementClass)
supports(Class)
returns true
for the JAXBElement
class.
Default is false
, meaning that supports(Class)
always returns false
for
JAXBElement
classes (though supports(Type)
can return true
, since it can obtain the
type parameters of JAXBElement
).
This property is typically enabled in combination with usage of classes like
MarshallingView
, since the ModelAndView
does not offer type parameter information at runtime.
supports(Class)
,
supports(Type)
public void setCheckForXmlRootElement(boolean checkForXmlRootElement)
supports(Class)
should check for
@XmlRootElement
annotations.
Default is true
, meaning that supports(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 to false
supports these
JAXB implementations.
supports(Class)
,
supports(Type)
public void setBeanClassLoader(java.lang.ClassLoader classLoader)
BeanClassLoaderAware
class 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
in interface BeanClassLoaderAware
classLoader
- the owning class loader; may be null
in
which case a default ClassLoader
must be used, for example
the ClassLoader
obtained via
ClassUtils.getDefaultClassLoader()
public void setResourceLoader(ResourceLoader resourceLoader)
ResourceLoaderAware
This might be a ResourcePatternResolver, which can be checked
through instanceof ResourcePatternResolver
. See also the
ResourcePatternUtils.getResourcePatternResolver
method.
Invoked after population of normal bean properties but before an init callback
like InitializingBean's afterPropertiesSet
or a custom init-method.
Invoked before ApplicationContextAware's setApplicationContext
.
setResourceLoader
in interface ResourceLoaderAware
resourceLoader
- ResourceLoader object to be used by this objectResourcePatternResolver
,
ResourcePatternUtils.getResourcePatternResolver(org.springframework.core.io.ResourceLoader)
public final void afterPropertiesSet() throws java.lang.Exception
InitializingBean
This method allows the bean instance to perform initialization only possible when all bean properties have been set and to throw an exception in the event of misconfiguration.
afterPropertiesSet
in interface InitializingBean
java.lang.Exception
- in the event of misconfiguration (such
as failure to set an essential property) or if initialization fails.protected javax.xml.bind.JAXBContext getJaxbContext()
private javax.xml.bind.JAXBContext createJaxbContextFromContextPath() throws javax.xml.bind.JAXBException
javax.xml.bind.JAXBException
private javax.xml.bind.JAXBContext createJaxbContextFromClasses() throws javax.xml.bind.JAXBException
javax.xml.bind.JAXBException
private javax.xml.bind.JAXBContext createJaxbContextFromPackages() throws javax.xml.bind.JAXBException
javax.xml.bind.JAXBException
private javax.xml.validation.Schema loadSchema(Resource[] resources, java.lang.String schemaLanguage) throws java.io.IOException, org.xml.sax.SAXException
java.io.IOException
org.xml.sax.SAXException
public boolean supports(java.lang.Class<?> clazz)
Marshaller
supports
in interface Marshaller
supports
in interface Unmarshaller
clazz
- the class that this marshaller is being asked if it can marshaltrue
if this marshaller can indeed marshal instances of the supplied class;
false
otherwisepublic boolean supports(java.lang.reflect.Type genericType)
GenericMarshaller
supports
in interface GenericMarshaller
supports
in interface GenericUnmarshaller
genericType
- the type that this marshaller is being asked if it can marshaltrue
if this marshaller can indeed marshal instances of the supplied type;
false
otherwiseprivate boolean supportsInternal(java.lang.Class<?> clazz, boolean checkForXmlRootElement)
private boolean isPrimitiveWrapper(java.lang.Class<?> clazz)
private boolean isStandardClass(java.lang.Class<?> clazz)
public void marshal(java.lang.Object graph, javax.xml.transform.Result result) throws XmlMappingException
Marshaller
Result
.marshal
in interface Marshaller
graph
- the root of the object graph to marshalresult
- the result to marshal toXmlMappingException
- if the given object cannot be marshalled to the resultpublic void marshal(java.lang.Object graph, javax.xml.transform.Result result, MimeContainer mimeContainer) throws XmlMappingException
MimeMarshaller
Result
,
writing binary data to a MimeContainer
.marshal
in interface MimeMarshaller
graph
- the root of the object graph to marshalresult
- the result to marshal tomimeContainer
- the MIME container to write extracted binary content toXmlMappingException
- if the given object cannot be marshalled to the resultprivate void marshalStaxResult(javax.xml.bind.Marshaller jaxbMarshaller, java.lang.Object graph, javax.xml.transform.Result staxResult) throws javax.xml.bind.JAXBException
javax.xml.bind.JAXBException
protected javax.xml.bind.Marshaller createMarshaller()
protected void initJaxbMarshaller(javax.xml.bind.Marshaller marshaller) throws javax.xml.bind.JAXBException
Marshaller
, and after the respective properties have been set.
The default implementation sets the defined properties
, the validation event handler
, the schemas
, listener
, and
adapters
.
javax.xml.bind.JAXBException
public java.lang.Object unmarshal(javax.xml.transform.Source source) throws XmlMappingException
Unmarshaller
Source
into an object graph.unmarshal
in interface Unmarshaller
source
- the source to marshal fromXmlMappingException
- if the given source cannot be mapped to an objectpublic java.lang.Object unmarshal(javax.xml.transform.Source source, MimeContainer mimeContainer) throws XmlMappingException
MimeUnmarshaller
Source
into an object graph,
reading binary attachments from a MimeContainer
.unmarshal
in interface MimeUnmarshaller
source
- the source to marshal frommimeContainer
- the MIME container to read extracted binary content fromXmlMappingException
- if the given source cannot be mapped to an objectprivate java.lang.Object unmarshalStaxSource(javax.xml.bind.Unmarshaller jaxbUnmarshaller, javax.xml.transform.Source staxSource) throws javax.xml.bind.JAXBException
javax.xml.bind.JAXBException
protected javax.xml.bind.Unmarshaller createUnmarshaller()
protected void initJaxbUnmarshaller(javax.xml.bind.Unmarshaller unmarshaller) throws javax.xml.bind.JAXBException
Marshaller
, and after the respective properties have been set.
The default implementation sets the defined properties
, the validation event handler
, the schemas
, listener
, and
adapters
.
javax.xml.bind.JAXBException
protected XmlMappingException convertJaxbException(javax.xml.bind.JAXBException ex)
JAXBException
to an appropriate exception from the
org.springframework.oxm
hierarchy.ex
- JAXBException
that occuredXmlMappingException