With the help of Dozer I want to map an Object[] to a DTO class
<mapping>
<class-a>com.example.myDtoClass</class-a>
<class-b>java.lang.Object[]</class-b>
<field>
<a>prop</a>
<!-- <b key="1">this</b> -->
<b>this[1]</b>
<b-hint>java.lang.String</b-hint>
</field>
</mapping>
but both settings <b>this[1]</b> and <b key="1">this</b> do not work and the prop field is set with the entire Object[] field so the result in returned JSON looks like
"prop": "[Ljava.lang.Object;#40147864"
what am I missing? I am expecting to have the 1 element of the Object[] in the prop variable
I made a bad mistake. Yes I get a Object[] to map, but the 1 element of that Object[] again is of type Object[]. I got confused and thought it was the main element. I did though expect a String, but got a array of binary code that has to be joined back to a string.
Related
I'm stuck on a wrong XML serialization I'm obtaining with Jackson XML mapper serializer.
I have a generic RESULT TAG that must contain some other elements, and these elements may be lists of elements, something like this:
<RESULT>
<THINGS>
<THING>
<SOMETHING_ABOUT_THING>001</SOMETHING_ABOUT_THING>
</THING>
<THING>
<SOMETHING_ABOUT_THING>002</SOMETHING_ABOUT_THING>
</THING>
</THINGS>
</RESULT>
But what I'm obtaining through standard XML mapper is something with an extra tag like this:
<RESULT>
<THINGS>
<THING>
<SOMETHING_ABOUT_THING>001</SOMETHING_ABOUT_THING>
</THING>
</THINGS>
<THINGS>
<THING>
<SOMETHING_ABOUT_THING>002</SOMETHING_ABOUT_THING>
</THING>
</THINGS>
</RESULT>
I do not have any custom object, because tag names and tag content are dynamically retrieved from a database, I'm just working with an HashMap, and trying to serialize it. The hashmap holds a single THINGS key that contains as value a list of maps, and each map contains a single THING element.
What can I do to achieve the first result?
Thanks in advance
How to map data from the collection in dozer. Sharing the following sample. Kindly help me with same.
<field>
<a>person.addresses[0].address</a>
<b>employee.address</b>
</field>
How to iterate pojo class and map data from source to destination ?
I am getting the following exception
Getting Mapping Exceptions org.dozer.MappingException: No read or write method found for field.
Try deep index mapping hint
<field>
<a>person.addresses[0].address</a>
<b>employee.address</b>
<a-deep-index-hint>org.yourpackage.person.entity.Address</a-deep-index-hint>
</field>
or
Check your setter methods, If you have Unusual setter/getter methods you can specify the custom setter/getter methods.
<field>
<a get-method="getAddress" >person.addresses[0].address</a>
<b set-method="setAddress" >employee.address</b>
</field>
I am using the DOM parser. I have to parse the following XML:
<abc>
<type action="">
<code>test</code>
<value>001</value>
</type>
<type action="">
<code>test2</code>
<value>002</value>
</type>
</abc>
so, depending on the value field under the type field, I have to fill in the action attribute in the type field. I am a bit stumped. I am able to get the value of the value field, but I don't know how to go back and add the attribute.
Any help will be appreciated a lot!!!
thanks!
To go back, just save a reference to the type Element before you traverse to its value child. (assuming you visited it already).
to change the value, use the setAttribute() method.
edit:
Alternate method: from the value text node, call getParentNode() twice (once to get back to the value element & once to get back to the type element), then call setAttribute() after you do any necissary casting.
try something like
nodelist = doc.getElementsByTagName("value");
for (Element element : nodelist) {
Element parent = element.getParentNode()
parent.setAttribute("action", "attrValue");
}
A buddy of mine asked me to post this question:
EDIT: He decided to post the question on his own here: JAXB Unmarshalls XML Incorrectly I tried to delete this question but couldn't.
I am trying to marshall a java class, called MyContainer. This class extends my another class, called Container. Container has a variable called: protected List<ResourceReference<Component>> child
The only variable MyContainer has is the one it inherits from Container. This List, however, will not marshall with JAXB when I have child listed as an XmlList.
It will marshall as an XmlElement, however this is not how I want the data displayed. When it is marshalled as an XmlElement, it lists the attributes in the List, but not 'heirarchially' It just lists the two elements as if they were properties of the class, as opposed to belonging to the child list.
How do I get Jaxb to marshall the List (Which is instantiated as an ArrayList) correctly?
EDIT: Here's an example:
This is what I am getting:
<MyContainer name="Container Name">
<booleanVal>false</booleanVal>
<child id="Test Comp 1"/>
<child id="Test Comp 2"/>
</and>
This is more what I expect:
<MyContainer name="Container Name">
<booleanVal>false</booleanVal>
<child> /// This would be the class variable of the extended Class, Container
<ResourceRef id="Entry 1">
<ResourceRef id="Entry 2">
</child>
</and>
Admittly, my current results is probably from the List in Container being marked with the #XmlElement annotation. However, JAXB will not marshall anything else..
Annotate your list with #XmlElementWrapper. See javadoc.
My castor masrshaller have an XML output as below
root>
field1 /field1>
field2 /field2>
..........
fieldn>
collection>
field>
field>
..........
field>
/collection>
/root>
my mapping for the collection part is
field name="collectionObj" type="string" collection="arraylist">
bind-xml name="field" location="collection" node="element" />
/field>
The issue is when the I will always have the collectiObj is empty
whole collection>tag disappears from the XMl output. Instead I would like to display as collection/>. Is there a way other than writing some fieldhandler, say setting some property to handle this issue.
Couldn't find any alternative other than a custom field handler.. It is working fine...