Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unmarshalling collections in JaxB

suppose I have this class:

public class A {

    private HashMap<String, B> map;

    @XmlElement
    private void setB(ArrayList<B> col) {
        ...
    }

    private ArrayList<B> getB() {
        ...
    }

}

When trying to unmarshall an xml document to this class using JaxB I notice that instead of calling the setB() method and sending me the list of B instances JaxB actually calls the getB() and adds the B instances to the returned list. Why?

The reason I want the setter to be called is that the list is actually just a temporary storage from which I want to build the map field, so I thought to do it in the setter.

Thanks.

like image 576
Stas Avatar asked Jun 23 '09 12:06

Stas


People also ask

What is JAXB unmarshalling?

The JAXB Unmarshaller interface is responsible for governing the process of deserializing the XML data to Java Objects. The unmarshalling to objects can be done to variety of input sources.

What is marshalling and unmarshalling XML?

Marshalling is the process of writing Java objects to XML file. Unmarshalling is the process of converting XML content to Java objects.

What is marshalling and unmarshalling in soap?

Unmarshalling - to convert XML data into JAXB-derived Java objects. Marshalling - to convert a JAXB-derived Java object tree into XML data.

How do you Unmarshal string JAXB?

To unmarshal an xml string into a JAXB object, you will need to create an Unmarshaller from the JAXBContext, then call the unmarshal() method with a source/reader and the expected root object.


2 Answers

thats the way jaxb is handling collections. you have to be sure you have a non null collection when jaxb try to unmarshal.

there is a plugin (never used it myself) but can be helpful: https://jaxb2-commons.dev.java.net/collection-setter-injector/

like image 142
silmx Avatar answered Sep 24 '22 19:09

silmx


Hy,

you can use it with jaxb, it's work !!! (with Maven....)

<plugin>
            <groupId>org.jvnet.jaxb2.maven2</groupId>
            <artifactId>maven-jaxb2-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <args>
                    <arg>-Xcollection-setter-injector</arg>
                </args>
                <plugins>
                    <plugin>
                        <groupId>net.java.dev.vcc.thirdparty</groupId>
                        <artifactId>collection-setter-injector</artifactId>
                        <version>0.5.0-1</version>
                    </plugin>
                </plugins>
                <schemaDirectory>src/schemas</schemaDirectory>
                <generateDirectory>src/main/java</generateDirectory>
                <extension>true</extension>
            </configuration>
        </plugin>

and you get your setter for your Collection

Hope it would help people

bye

like image 20
LE GALL Benoît Avatar answered Sep 21 '22 19:09

LE GALL Benoît