...
To explain how Dozer can be uses within Camel we'll use the following example of a simple Customer Support Service. The initial version of the Service defined a 'Customer' object used with a very flat structure.
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
public class Customer { private String firstName; private String lastName; private String street; private String zip; public Customer() {} public Customer(String firstName, String lastName, String zip, String street) { this.firstName = firstName; this.lastName = lastName; this.zip = zip; this.street = street; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } ... getters and setters for each field |
In the next version it was decided to structure the data better in the model by moving the address data into its own type, with the resultin domain object ending up looking like
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
public class Customer { private String firstName; private String lastName; private Address address; public Customer() {} public Customer(String firstName, String lastName, Address address) { this.firstName = firstName; this.lastName = lastName; this.address = address; } .... public class Address { private String zipCode; private String streetName; public Address() {} public Address(String zipCode, String streetName) { this.zipCode = zipCode; this.streetName = streetName; } |
...
Dozer's configuration is extremely flexible and many mapping scenarios are covered here. For our simple example, the configuration looks like the following.
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
<mappings xmlns="http://dozer.sourceforge.net" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://dozer.sourceforge.net http://dozer.sourceforge.net/schema/beanmapping.xsd"> <mapping> <class-a>org.apache.camel.converter.dozer.service.Customer</class-a> <class-b>org.apache.camel.converter.dozer.model.Customer</class-b> <field> <a>street</a> <b>address.streetName</b> </field> <field> <a>zip</a> <b>address.zipCode</b> </field> </mapping> </mappings> |
...