Since we're on a major migration process of this website, some component documents here are out of sync right now. In the meantime you may want to look at the early version of the new website
https://camel.apache.org/staging/
We would very much like to receive any feedback on the new site, please join the discussion on the Camel user mailing list.
JAXBJAXB is a Data Format which uses the JAXB2 XML marshalling standard which is included in Java 6 to unmarshal an XML payload into Java objects or to marshal Java objects into an XML payload. Using the Java DSLFor example the following uses a named DataFormat of jaxb which is configured with a number of Java package names to initialize the JAXBContext. You can if you prefer use a named reference to a data format which can then be defined in your Registry such as via your Spring XML file. e.g. Using Spring XMLThe following example shows how to use JAXB to unmarshal using Spring configuring the jaxb data type It is possible to use this data format with more than one context path. You can specify context path using Partial marshalling/unmarshallingThis feature is new to Camel 2.2.0. FragmentThis feature is new to Camel 2.8.0. Ignoring the NonXML CharacterThis feature is new to Camel 2.2.0.
This feature has been tested with Woodstox 3.2.9 and Sun JDK 1.6 StAX implementation. New for Camel 2.12.1 The following example shows using the Spring DSL and also enabling Camel's NonXML filtering: Working with the ObjectFactoryIf you use XJC to create the java class from the schema, you will get an ObjectFactory for you JAXB context. Since the ObjectFactory uses JAXBElement to hold the reference of the schema and element instance value, jaxbDataformat will ignore the JAXBElement by default and you will get the element instance value instead of the JAXBElement object form the unmarshaled message body. Setting encodingYou can set the encoding option to use when marshalling. Its the In this Spring DSL we have defined to use Controlling namespace prefix mappingAvailable as of Camel 2.11 When marshalling using JAXB or SOAP then the JAXB implementation will automatic assign namespace prefixes, such as ns2, ns3, ns4 etc. To control this mapping, Camel allows you to refer to a map which contains the desired mapping. Notice this requires having JAXB-RI 2.1 or better (from SUN) on the classpath, as the mapping functionality is dependent on the implementation of JAXB, whether its supported. For example in Spring XML we can define a Map with the mapping. In the mapping file below, we map SOAP to use soap as prefix. While our custom namespace "http://www.mycompany.com/foo/2" is not using any prefix. To use this in JAXB or SOAP you refer to this map, using the Schema validationAvailable as of Camel 2.11 The JAXB Data Format supports validation by marshalling and unmarshalling from/to XML. Your can use the prefix classpath:, file:* or *http: to specify how the resource should by resolved. You can separate multiple schema files by using the ',' character. Camel 2.11.0 and 2.11.1 has a known issue by validation multiple Using the Java DSL, you can configure it in the following way: You can do the same using the XML DSL: Camel will create and pool the underling Schema LocationAvailable as of Camel 2.14 The JAXB Data Format supports to specify the SchemaLocation when marshaling the XML. Using the Java DSL, you can configure it in the following way: You can do the same using the XML DSL: Marshal data that is already XMLAvailable as of Camel 2.14.1 The JAXB marshaller requires that the message body is JAXB compatible, eg its a JAXBElement, eg a java instance that has JAXB annotations, or extend JAXBElement. There can be situations where the message body is already in XML, eg from a String type. There is a new option mustBeJAXBElement you can set to false, to relax this check, so the JAXB marshaller only attempts to marshal JAXBElements (javax.xml.bind.JAXBIntrospector#isElement returns true). And in those situations the marshaller fallbacks to marshal the message body as-is.XmlRootElement objectsAvailable as of Camel 2.17.2 The JAXB Data Format option objectFactory has a default value equals to false. This is related to a performance degrading. For more information look at the issue CAMEL-10043 For the marshalling of non-XmlRootElement JaxB objects you'll need to call JaxbDataFormat#setObjectFactory(true) DependenciesTo use JAXB in your camel routes you need to add the a dependency on camel-jaxb which implements this data format. If you use maven you could just add the following to your pom.xml, substituting the version number for the latest & greatest release (see the download page for the latest versions). |