I am trying to convert two classes that have an attribute of type java.util.List, but one of the classes does not have a method "set*" only the method "get*" like this:
<mapping>
<class-a>com.mycompany.bean.SocioPj</class-a>
<class-b>com.mycompany.jaxb.SocioPjXml</class-b>
<field>
<a set-method="getListaSocios().add">listaSocios</a>
<b>listaSocios</b>
<a-hint>com.mycompany.bean.SocioPf,com.mycompany.bean.SocioPj</a-hint>
<b-hint>com.mycompany.jaxb.SocioPfXml,com.mycompany.jaxb.SocioPjXml</b-hint>
</field>
</mapping>
The example above the class: SocioPj doesn´t have a method setListSocios() when I tried to convert I catch this error:
[Time:2015-03-24 11:29:08,055][Level:ERROR][Thread:http-bio-8080-exec-10][OnboardingCCMFulfillmentImpl] org.dozer.MappingException: java.lang.ClassNotFoundException: /add
Has anyone faced this problem?
You can tell Dozer to directly access the field without calling the getter/setter (Even if the field is private):
<field>
<a is-accessible="true">listaSocios</a>
<b>listaSocios</b>
</field>
Related
'<mapping map-id="vehicle Info">
<class-a>vehicle Info</class-a>
<class-b>uninsured</class-b>
<field>
<a>CONSTANT_CAR</a>
<b>car.value</b>
</field>
</mapping>'
I'm trying to send the constant value of "CONSTANT_CAR" into the field of "car.value" which is located inside the vehicle
Can someone please guide me how can I pass the constant value to a setter which is located in the destination class.
You should be able to access the constant by setting the 'is-accessible="true"' for the constant field. So, your mapping could look something like this:
<mapping map-id="vehicle-info">
<class-a>path.to.VehicleInfo</class-a>
<class-b>path.to.Uninsured</class-b>
<field>
<a is-accessible="true">CONSTANT_CAR</a>
<b>car.value</b>
</field>
</mapping>
Make sure when naming the classes in the <class-a> and <class-b> tags you put the full path to the class.
I just tested with some sample code and it mapped the constant as expected. Let me know if it works for you!
Does Dozer could call custom converter after default?
I want to create something like chain. At first I want to call default converter to make most of convertion work and only after that call custom converter to populate complex fields?
EDIT
by default when I create custom converter I need to override two methods. Each of this method has 2 arguments source object and target object. But when we call convertTo method second argument(target object) equals to null. So may be I need to specify something in mapping file to make dozer process default converter before custom?
Sorry for bad english
After look into source code I understand that this probably imposible. Instead of this dozer library propose using custom-converters at field level like this:
<mapping wildcard="false" >
<class-a>package.A</class-a>
<class-b>package.B</class-b>
<field>
<a>id</a>
<b>id</b>
</field>
<field custom-converter="test.AtoBNameFieldCustomConverter">
<a>name</a>
<b>name</b>
</field>
</mapping>
May be this help someone.
I am working on writing a dozer mapping to map fields from ClassA to ClassB. I have two use cases to address:
Use Case 1: Map all fields from ClassA to ClassB including "null" fields. This way I would be able to wipe out the value of a field from ClassB if it doesn't have a value in ClassA.
Use Case 2: Map only "not null" fields from ClassA to ClassB. This way I would be able to preserve fields in ClassB even when they don't have values in ClassA.
Currently, I have to write two different mappings to address this situation. The mappings are exactly alike, apart from the value of the attribute "map-null".
Question:
Is there a way to avoid this duplication of mappings and set the "map-null" attribute at runtime and just have one generic mapping defined in Dozer?
<mapping map-id="myMappings1" map-null="true">
<class-a>test.ClassA</class-a>
<class-b>test.ClassB</class-b>
<field>
<a>sampleVariable</a>
<b>samplevariable</b>
</field>
</mapping>
<mapping map-id="myMappings2" map-null="false">
<class-a>test.ClassA</class-a>
<class-b>test.ClassB</class-b>
<field>
<a>sampleVariable</a>
<b>samplevariable</b>
</field>
</mapping>
I'd like to map a CustomConverter in dozer but I'd like to pass the whole current object as the source. All of the examples in the dozer CustomConverter documentation pass a field of the input object as the source and not the whole object.
I'd like to do something like this:
<mapping>
<class-a>foo.bar.InputObject</class-a>
<class-b>foo.bar.OutputObject</class-b>
<field custom-converter="foo.bar.MyConverter">
<a>this</a> <!-- how do I access the whole value and not just a field? -->
<b>custom</b>
</field>
<field>
<a>anotherField</a>
<b>anotherField</b>
</field>
</mapping>
And
public class MyConverter extends DozerConverter<InputObject, String> {
...
public String convertTo(InputObject input, String custom) {
// do some transformation
}
}
CustomConverter docs here:
http://dozer.sourceforge.net/documentation/customconverter.html
Try implementing the CustomConverter instead of DozerConverter and try passing it as:
<field custom-converter="my.custom.converter">
<a>this</a>
<b>myfield</b>
</field>
If you use the field mapping you want to identify the attribute by using "key":
<field custom-converter="de.xyz.custom.MyConverter">
<a key="variablename">this</a>
<b>targetvariablename</b>
</field>
You can then proceed to implement the converter. You will be given the Object containing the field "variablename" as source. If for example you do not have a setter for a list value (like I had for whatever reason...) you are now able to manipulate the source object the way you need to.
I use Dozer to map between Java beans. I have a dozer mapping file with all the mappings present in it. A sample mapping is shown below:
<mapping>
<class-a>com.xyz.A</class-a>
<class-b>com.xyz.B</class-b>
<field>
<a>key</a>
<b>id</b>
</field>
<field>
<a>actionId</a>
<b>action</b>
</field>
</mapping>
Now I want to find the mappings at runtime. For example, give a field as a string com.xyz.A.key, I want to find that it maps to com.xyz.B.id programatically. I looked into the Dozer API (org.dozer.Mapper interface) and it exposes methods only to map beans.
So my question is: is there an API to find the mappings programatically?